[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# how to print out a byte array in hex form

Posted on 2007-11-28
Medium Priority
11,147 Views
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
Question by:mmingfeilam
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 92

Accepted Solution

objects earned 270 total points
ID: 20371478
following shows two alternatives

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

LVL 9

Assisted Solution

ysnky earned 240 total points
ID: 20371711
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;
}
``````
0

LVL 86

Assisted Solution

CEHJ earned 240 total points
ID: 20371756
>>http://www.objects.com.au/java/qa/1237178758.html

Hmm - think i'll have to start charging commission ;-)
0

LVL 86

Expert Comment

ID: 20895235
:-) Oh maybe i got a commission of some sort ;-)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
###### Suggested Courses
Course of the Month14 days, 16 hours left to enroll