Solved

how to print out a byte array in hex form

Posted on 2007-11-28
4
11,107 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
[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
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

695 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