• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 11157
  • Last Modified:

how to print out a byte array in hex form

if i have a byte array:

byte[] bytes = new byte[2];
bytes[0] = 'a';
bytes[1] = '1;

how do you print it out in hex form?
0
mmingfeilam
Asked:
mmingfeilam
  • 2
3 Solutions
 
objectsCommented:
following shows two alternatives

http://www.objects.com.au/java/qa/1237178758.html
0
 
ysnkyCommented:
for more info;
http://www.devx.com/tips/Tip/13540
  public static String byteArrayToHexString(byte in[]) {
 
	    byte ch = 0x00;
	    int i = 0; 
 
	    if (in == null || in.length <= 0)
	        return null;
 
	    String pseudo[] = {"0", "1", "2","3", "4", "5", "6", "7", "8","9", "A", "B", "C", "D", "E","F"};
	    StringBuffer out = new StringBuffer(in.length * 2);
 
	    while (i < in.length) {
 
	        ch = (byte) (in[i] & 0xF0); // Strip off high nibble
	        ch = (byte) (ch >>> 4);	     // shift the bits down
	        ch = (byte) (ch & 0x0F);    //	must do this is high order bit is on!
 
	        out.append(pseudo[ (int) ch]); // convert the nibble to a String Character
	        ch = (byte) (in[i] & 0x0F); // Strip off low nibble 
	        out.append(pseudo[ (int) ch]); // convert the nibble to a String Character
	        i++;
	    }
 
	    String rslt = new String(out);
	    return rslt;
	}      

Open in new window

0
 
CEHJCommented:
>>http://www.objects.com.au/java/qa/1237178758.html

Hmm - think i'll have to start charging commission ;-)
0
 
CEHJCommented:
:-) Oh maybe i got a commission of some sort ;-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now