Solved

how to print out a byte array in hex form

Posted on 2007-11-28
4
11,092 Views
Last Modified: 2009-11-25
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
Comment
Question by:mmingfeilam
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 90 total points
ID: 20371478
following shows two alternatives

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

Assisted Solution

by:ysnky
ysnky earned 80 total points
ID: 20371711
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 80 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

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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 85
How to convert String matching to regex in java 4 34
expectj telnet failing 5 37
iterator/ListIterator approach 17 28
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question