469,306 Members | 1,987 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Binary byte[] buffer to hex byte[] buffer conversion

Hello,

I might have a bad day today but I have a byte[] buffer holding binary
numbers. Now, I want to convert this to byte[] buffer with hex numbers. Is
there any elegant way of doing that?

Some method like: byte[] bin2hex(byte[]) {}

BRs,
Zulik
Jul 17 '05 #1
5 17125
Zulik <zu*****@yahoo.com> wrote in message news:<pa****************************@yahoo.com>...
Hello,

I might have a bad day today but I have a byte[] buffer holding binary
numbers. Now, I want to convert this to byte[] buffer with hex numbers. Is
there any elegant way of doing that?

Some method like: byte[] bin2hex(byte[]) {}

BRs,
Zulik

Hex representation of number can't be Java byte type. And whatever the
representation format would be, the value of the number is same. 0x20
is 32 is 00100000.
Jul 17 '05 #2
"Zulik" <zu*****@yahoo.com> ha scritto nel messaggio news:pa****************************@yahoo.com...
Some method like: byte[] bin2hex(byte[]) {}


maybe did you intended "String bin2hex(byte[])" ?

if it was so, (Sorry for my english: I'm translating "word to word"
from Italian lang. :-) )
here you are the following code:

public class HexTool {

static private String[] HEX = {"0","1","2","3","4","5","6","7","8","9","A","B"," C","D","E","F"};

static public String hexify(byte byValue){
int nValue = (int)byValue + 128;
return HEX[nValue/16]+HEX[nValue%16];
}

static public String hexify(byte[] bytes){
String hexed ="";
for(int i = 0; i<bytes.length; i++){
hexed += hexify(bytes[i])+" ";
}
return hexed;
}
}

Bye,
GianpieroP
Jul 17 '05 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

GianpieroP wrote:
"Zulik" <zu*****@yahoo.com> ha scritto nel messaggio
news:pa****************************@yahoo.com...
Some method like: byte[] bin2hex(byte[]) {}


maybe did you intended "String bin2hex(byte[])" ?

[snippage of a bin2hex algorithm]

Hi,
Or, even more concise, how about a call to
java.lang.{Integer|Long}.toHexString()?

Heh... don't re-invent the wheel :)

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQFAMVtDnwjA8LryK2IRAruGAKC0WUM/TCKGjZqlE+4yzc8gDJKRcACg+JPv
OAt8ElsdjczluSCoUkH1zMM=
=KG6S
-----END PGP SIGNATURE-----
Jul 17 '05 #4
"Chris" <ch*******@hotmail.com> ha scritto nel messaggio news:UzfYb.18042$Hy3.8786@edtnps89...
Hi,
Or, even more concise, how about a call to
java.lang.{Integer|Long}.toHexString()?

Heh... don't re-invent the wheel :)

Ok! So my class loses one method: "... hexify(byte byValue)" and one member: "HEX".
But my "wheel" is useful again because it presents "String hexify(byte[] byArr)" static method.
However I'm happy if you offers me another existent "wheel" in substitution of my hexify(byte[] ) method.

Bye bye,
GianpieroP
Jul 17 '05 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

GianpieroP wrote:
"Chris" <ch*******@hotmail.com> ha scritto nel messaggio
news:UzfYb.18042$Hy3.8786@edtnps89...
Hi,
Or, even more concise, how about a call to
java.lang.{Integer|Long}.toHexString()?

Heh... don't re-invent the wheel :)

Ok! So my class loses one method: "... hexify(byte byValue)" and one
member: "HEX". But my "wheel" is useful again because it presents
"String hexify(byte[] byArr)" static method. However I'm happy if
you offers me another existent "wheel" in substitution of my
hexify(byte[] ) method.

Bye bye,
GianpieroP


Heh... actually, if working with large values, how about:

public static String hexify(byte[] data) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < data.length; i++) {
sb.append(Integer.toHexString(((int) data[i]) & 0xFF));
}
return sb.toString();
}

The use of StringBuffer is the real point I'm making here, since it's
more efficient than repeated String concatenations. Also, since
Integer.toHexString() takes an int, you have to do the casting
oddness to prevent, say, -1 from turning into 0xFFFFFFFF. Other than
that, your method and mine are about the same. So that's my 2-cent
wheel :)

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQFAMqqQnwjA8LryK2IRAiX5AJ4yBjkHA7uH/iwuPcee5upCBoPjUACfXqYH
bTDHAQpDgKzGA4/uCERa7sE=
=z+U/
-----END PGP SIGNATURE-----
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by J. Campbell | last post: by
7 posts views Thread by Drake | last post: by
2 posts views Thread by phyzics | last post: by
26 posts views Thread by Patient Guy | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.