Home arrow static arrow Java Programming [Archive] - Byte array to hex form
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Byte array to hex form
This topic has 10 replies on 1 page.

Posts:21
Registered: 8/27/03
Byte array to hex form  
Apr 26, 2004 4:14 PM



 
Hello,
Simple question (at least I hope so); I have an array of bytes from a MessageDigest and would like to know how to convert that array to hexadecimal form. I looked through the APIs and wasn't able to find what I was looking for.

Any help would be appreciated.

Thanks,
- Smiley
 

Posts:152
Registered: 3/23/04
Re: Byte array to hex form  
Apr 26, 2004 4:18 PM (reply 1 of 10)



 
you mean convert the element of array to hexa right ?
 

Posts:21
Registered: 8/27/03
Re: Byte array to hex form  
Apr 26, 2004 4:19 PM (reply 2 of 10)



 
Yes, convert the entire array to hexadecimal form.
 

Posts:37,103
Registered: 3/30/99
Re: Byte array to hex form  
Apr 26, 2004 4:22 PM (reply 3 of 10)



 
Do you mean a string representation?
 

Posts:152
Registered: 3/23/04
Re: Byte array to hex form  
Apr 26, 2004 4:23 PM (reply 4 of 10)



 
I'm afraid that you've got to write the method yourself. I don't think we have it in API.
 

Posts:21
Registered: 8/27/03
Re: Byte array to hex form  
Apr 26, 2004 4:44 PM (reply 5 of 10)



 
Can someone give me some pointers as to how to do this then? I'm not as experienced in this general area..

Thanks,
- Smiley
 

Posts:2,830
Registered: 9/1/03
Re: Byte array to hex form  
Apr 26, 2004 4:45 PM (reply 6 of 10)



 
look at the Integer class ...
 

Posts:152
Registered: 3/23/04
Re: Byte array to hex form  
Apr 26, 2004 4:59 PM (reply 7 of 10)



 
Yeah, that's great. So you just cast each element to type of int and using Integer.toHexString(..)

If you're still wanting to write it yourself, so here's one way:
Assuming you want to translate a number A to Hex:
- Keep dividing A by 16 until get 0.
- The String of all remainder writting in reversed order is what you need.
 

Posts:21
Registered: 8/27/03
Re: Byte array to hex form  
Apr 26, 2004 5:05 PM (reply 8 of 10)



 
Hey,
I've got it finished.. here's what I came out with:

import java.security.*; public class md {  public static void main(String[] args) { try {  MessageDigest md = MessageDigest.getInstance("MD5");  String str = "mystring";  byte[] byt = str.getBytes();  md.update(byt);  byte[] finished = md.digest();  String total = "";  for (int i=0;i<16;i++) {   String temp = Integer.toHexString(finished[i]);   if (finished[i] < 0) {    temp = temp.substring(6);   }   total += ""+temp;  }  System.out.println("number is "+total); } catch (NoSuchAlgorithmException nsae) {} } }


Thanks to all for the help!

- Smiley
 

Posts:2
Registered: 5/11/99
Re: Byte array to hex form  
Jun 28, 2004 12:57 AM (reply 9 of 10)



 
What about this?

<pre>

private static final char CODE[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};

public String toHexString(byte bytes[])
{
char chars[] = new char[bytes.length*2];

for (int ii = 0 ; ii < bytes.length ; ++ ii)
{
chars[ii] = CODE[(bytes[ii] & 0xF0) >>> 4];
chars[ii+1] = CODE[bytes[ii] & 0x0F];
}
return new String(chars);
}

</pre>
 

Posts:2
Registered: 5/11/99
Correction: Re: Byte array to hex form   
Jun 28, 2004 1:52 AM (reply 10 of 10)



 
Sorry, must be:

            chars[2*ii]   = CODE[(bytes[ii] & 0xF0) >>> 4];            chars[2*ii+1] = CODE[bytes[ii] & 0x0F];
 
This topic has 10 replies on 1 page.