1

i m new to java. i want to convert a byte array of decimal value to hexadecimal string. my input byte array is [0, 0, 0, 0, 0, 0, 1, -28]. i m getting 00000000000001e4 instead of 0000001e4. plz help me to solve this problem

 public static String ConvetToHex(byte[] decValue) 
{

    String value = "";
    for(int i = 0;i<decValue.length;i++)
    {
         value = value+ Integer.toString((decValue[i] & 0xff) + 0x100, 16).substring(1);
    }
    return value;
}
Sachin K S
  • 97
  • 1
  • 4
  • 12

1 Answers1

2

It looks correct to me. Eight bytes should turn into 16 hex characters. You can use

return new BigInteger(1, decValue).toString(16);

but it will produce the same output.

Peter Lawrey
  • 513,304
  • 74
  • 731
  • 1,106