2 Replies Latest reply: Jun 29, 2008 8:43 AM by 807601 RSS

    String vs Integer.toHexString

    807601
      I get raw packets from server. But i cant understand why string ab != cd, where its not printing Found: b5. what is the logic for String vs integer.toHexString ?
                      System.out.println("From server....");
                for(int j=0;j<st.length;j++){
                          if (j==1){
                              String ab = "ffffffb5";
                              String cd= Integer.toHexString(st[j]);
                              System.out.println("ab value/lenght: " + ab + "/" + ab.length());
                              System.out.println("cd value/lenght: " + cd + "/" +  cd.length());
                              
                              if( ab==cd ){
                                  System.out.println("Found: b5");
                              }else{
                                  System.out.println("ab!= cd");
                              } //End of if
                          }//End of if
                } //End of for
      compile:
      run:
      From server....
      ab value/lenght: ffffffb5/8
      cd value/lenght: ffffffb5/8
      ab!= cd
      BUILD SUCCESSFUL (total time: 0 seconds)

      Edited by: shamuntoha on Jun 29, 2008 3:32 PM
        • 1. Re: String vs Integer.toHexString
          807601
          When comparing strings, use .equals(myString) instead of the == operator.

          example:
          String a = "fred";
          String b = "ned";
          
          System.out.println("Are they equal? " + a.equals(b));
          • 2. Re: String vs Integer.toHexString
            807601
            Great, thanks, correct method to use "myString.equals(compareString); return true or false"
                            System.out.println("From server....");
                      for(int j=0;j<st.length;j++){
                                if (j==1){
                                    String ab = "ffffffb5";
                                    String cd= Integer.toHexString(st[j]);
                                    System.out.println("ab value/lenght: " + ab + "/" + ab.length());
                                    System.out.println("cd value/lenght: " + cd + "/" +  cd.length());
                                    System.out.println("Are they equal? " + ab.equals(cd));
            
                                    if( ab.equals(cd) ){
                                        System.out.println("Found: b5");
                                    }else{
                                        System.out.println("ab!= cd");
                                    } //End of if
                                }//End of if
                      } //End of for
            compile:
            run:
            From server....
            ab value/lenght: ffffffb5/8
            cd value/lenght: ffffffb5/8
            Are they equal? true
            Found: b5
            BUILD SUCCESSFUL (total time: 0 seconds)