2

I have a byte array that I want to convert its value in hexadecimal.

byte array = [48, 48, 28, ...]

--->

hex byte array = [30, 30, 1C, ...]

Maroun
  • 91,013
  • 29
  • 181
  • 233
Med Besbes
  • 1,941
  • 6
  • 23
  • 37
  • Here is a solution: http://stackoverflow.com/questions/9655181/convert-from-byte-array-to-hex-string-in-java Just don't return a String of the array, but rather the converted array itself – Lennart Apr 15 '13 at 10:12
  • There is no such thing as a 'hex byte array'. Hex is how you view it, print it, report it. Not a real question. – user207421 Apr 15 '13 at 10:15
  • why is this tagged with android? – Marco Forberg Apr 15 '13 at 10:21

2 Answers2

3

This should work. Maybe you have to convert byte to int if its not casted implicitly.

String[] hexArray = new String[byteArray.length];
for(int index = 0; index < byteArray.length; index++) {
    hexArray[index] = Integer.toHexString(byteArray[index]);
    // maybe you have to convert your byte to int before this can be done
    // (cannot check reight now)
}
Marco Forberg
  • 2,617
  • 5
  • 21
  • 33
  • hexadecial is only the representation. an array of byte will always have value from 0 to 255 and not display as 00 to FF unless you convert it to a String representation like with the `Integer.toHexString()` method – Marco Forberg Apr 15 '13 at 10:43
1

check Integer.toHexString method. iT will convert an int to a hex string. so iterate through your array and convert each number.

stinepike
  • 52,673
  • 14
  • 90
  • 109