简体   繁体   中英

Java string hex to int

I want to convert the below String hex to an Int (16).

a6 5f de 57

What have I done, but I don't know it's the right way and I can't verify if the number is correct or not.

byte[] rec = new byte[20];                  
DatagramPacket pRec = new DatagramPacket(rec, rec.length);
socket.receive(pRec);

String tData = "";
for(int i = 0; i < 20; i++){
   tData += String.format("%02x", rec[i]);
}

The output from the packet:

ffffffff730aa65fde5700000000000000000000

And now i must skip the first 6 bytes [ffffffff730a] and I need the remaining 4 bytes [a65fde57]. Note: [00000000000000000000] are NULL because the length of the buffer.

System.out.println( hexToInt(tData, 6, 10) );

private Integer hexToInt(String str, int start, int end){
   String t = str.substring(start, end);

   char[] ch = t.toCharArray();
   String res = "";
   for(int i = 0; i < end-start; i += 2){
      res += Integer.parseInt(ch[i]+ch[i+1]+"", 16);
   }

   return Integer.parseInt(res);
}

And the result is:

516262

Is this the right way to convert a hex String to an int? Is the result are correct?

Simplest thing would be to use something like int i = Integer.parseInt(...). See Integer.parseInt(java.lang.String, int)

I'd personally go this route:

private int hexToInt(String str, int start, int end) {
    String t = str.substring(start, end);

    byte[] bytes = new byte[4];
    int i,j = 0
    while (i < 8)
    {
        bytes[j] = Byte.valueOf(t.substring(i,i+1), 16);
        i+=2; 
        j++;
    }

    ByteBuffer bb = ByteBuffer.wrap(bytes);
    // That's big-endian. If you want little endian ...
    bb.order(ByteOrder.LITTLE_ENDIAN);
    return bb.getInt();
}

You could of course trim this down one level by only passing in the substring containing the 8 characters you want to convert.

No, that is not the right way, and that is not the right answer. What you are doing is converting the bytes to values but converting them back to decimal value string, and concatenating those together. Then at the end you convert back to numeric format. Don't convert back and forth so much, convert once to numeric and keep it there. Something like this:

private Integer hexToInt(String str, int start, int end){
    long res = 0;
    for(int i = start; i < end; i++){
        res = res*16  + Integer.parseInt(str.substring(i,i+1), 16);
    }
    return res;
}
private int convertHexStringToInt(String hexStr)
{
   int iValue= Integer.parseInt(hexStr,16); //base 16 for converting hexadecimal string

   return iValue;
}

Remember if the Hex String Format is prefixed by "0x" like "0xc38" then please remove the 0x from that Hex String.(I suggested this to do to avoid NumberFormatException)

You can use following code to do so

String arrHexStr[] = hexStr.split("x");

if(arrHexStr.length==2)
hexStr = arrHexStr[1]; // this will assign the "c38" value with removed "0x" characters

So the final function would look like this

private int convertHexStringToInt(String hexStr)
{
   String arrHexStr[] = hexStr.split("x");

   if(arrHexStr.length==2)
      hexStr = arrHexStr[1]; // this will assign the correct hex string "c38" value with removed prefixes like "0x" 

   int iValue= Integer.parseInt(hexStr,16); //base 16 for converting hexadecimal string

   return iValue;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM