0
private static byte[]theTestText ={0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,(byte) 0x88,(byte) 0x99,(byte) 0xAA,(byte) 0xBB,(byte) 0xCC,(byte) 0xDD,(byte) 0xEE,(byte) 0xFF};

String str= new String(theTestText); // converted the byte array to string
char ch = str.charAt(8); // extracting a specific character from string

I want to obtain the hex value from the character 'ch' as defined in the original byte array i.e. 0x88. Is there any way to get that?

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Yes there is. Convert the byte to a hex string. Have a look in one of those answers (as it's asked quite often) https://stackoverflow.com/search?q=[java]+byte+to+hex – SubOptimal Mar 23 '15 at 10:52
  • 1
    Is that arbitrary binary data? If it's *actually* text, what text encoding was it encoded with? – Jon Skeet Mar 23 '15 at 10:52
  • Similar Question has been Answered here Have a Look [Java code To convert byte to Hexadecimal][1] [1]: http://stackoverflow.com/questions/2817752/java-code-to-convert-byte-to-hexadecimal – Ankur Anand Mar 23 '15 at 10:53

1 Answers1

0

To get the hexa value in a string:

String str= new String(theTestText); // converted the byte array to string
char ch = str.charAt(8); // extracting a specific character from string
String hex = String.format("%04x", (int) ch); // Converting the char to a string with its hexadecimal value
Manuel Selva
  • 18,554
  • 22
  • 89
  • 134