472,328 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 17303
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: J. Campbell | last post by:
OK...I'm in the process of learning C++. In my old (non-portable) programming days, I made use of binary files a lot...not worrying about endian...
7
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function...
1
by: rusttree | last post by:
I'm working on a program that manipulates bmp files. I know the offset location of each piece of relevent data within the bmp file. For example,...
7
by: Drake | last post by:
Well, I'm stuck in legacy land and I need a helping hand. We're trying to give some modern value-added functionality to a circa-1985 fortran...
2
by: phyzics | last post by:
I am porting an application from C++ to C#, and am having trouble finding a way to quickly and efficiently write structures to a binary file. In C++...
6
by: John Hoffman | last post by:
Reading registry: .... RegistryKey rksub = rkey.OpenSubKey(s); String valstr = rksub.GetValueNames(); foreach (String vs in valstr) { String...
3
by: poifull | last post by:
Hi All, What is the proper way to read a binary file into a byte? I am using BinaryReader to read from a Stream and call the ReadByte method of...
26
by: Patient Guy | last post by:
Has anyone written code that successfully manipulates binary file data using Javascript? It might---and in the case of doing I/O, will---make use...
2
by: DBuss | last post by:
OK, I'm reading a multicast socket. It attaches fine, reads fine, all of that. The problem is that while some of the data I get is normal text...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.