473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 17384
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 issues. I'm starting to understand why C++ makes...
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 accepts input as 32 bit unsigned longs (DWORD). I...
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, I know the 18th through 21st byte is an integer...
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 proggie. The program produces a binary file, by...
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++ this is trivial because all that is necessary is...
6
by: John Hoffman | last post by:
Reading registry: .... RegistryKey rksub = rkey.OpenSubKey(s); String valstr = rksub.GetValueNames(); foreach (String vs in valstr) { String vstr = rksub.GetValue(vs).ToString(); OR 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 the BinaryReader object. The method I'm using...
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 of browser- specific functions (ActiveX/COM with...
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 (ASCII String), some of it is Binary Integer. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.