byte[] - hexadecimal conversion
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   chowdary_maddipati
Posted On:   Thursday, May 31, 2001 10:30 AM

How can i convert a byte[] to hexadecimal string.

Re: byte[] - hexadecimal conversion

Posted By:   Anonymous  
Posted On:   Thursday, May 31, 2001 11:33 AM

You can convert the byte[] array into an int (or a long, depends on the size) and then use Integer.toHexString() to get the requested output:

...
byte[] arr = {
(byte)2, (byte)11,
(btye)255, (byte)0
};
int val = 0;
for (int i=0; i val += arr[i] << (8 * i);
}
System.out.println("This is it: " + Integer.toHexString(val).toUpperCase());
...

Probably somebody else will find faster and cleanier solutions. I haven't tested this code, I've just wrote it on here, so there may be errors.

Regards

Re: byte[] - hexadecimal conversion

Posted By:   Jeff_Boyle  
Posted On:   Thursday, May 31, 2001 10:58 AM

See the following Url:

http://www.concentric.net/~jsboyle/byteToHexString.shtml

On the page, I document two different approaches which can be used (one was actually included in the JavaPRO magazine.

Jeff
About | Sitemap | Contact