By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
"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

P: n/a
-----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

P: n/a
"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

P: n/a
-----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.