473,587 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17407
Zulik <zu*****@yahoo. com> wrote in message news:<pa******* *************** ******@yahoo.co m>...
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.{Inte ger|Long}.toHex String()?

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

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

iD8DBQFAMVtDnwj A8LryK2IRAruGAK C0WUM/TCKGjZqlE+4yzc8 gDJKRcACg+JPv
OAt8ElsdjczluSC oUkH1zMM=
=KG6S
-----END PGP SIGNATURE-----
Jul 17 '05 #4
"Chris" <ch*******@hotm ail.com> ha scritto nel messaggio news:UzfYb.1804 2$Hy3.8786@edtn ps89...
Hi,
Or, even more concise, how about a call to
java.lang.{Inte ger|Long}.toHex String()?

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*******@hotm ail.com> ha scritto nel messaggio
news:UzfYb.1804 2$Hy3.8786@edtn ps89...
Hi,
Or, even more concise, how about a call to
java.lang.{Inte ger|Long}.toHex String()?

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(Integ er.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.toHexSt ring() 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)

iD8DBQFAMqqQnwj A8LryK2IRAiX5AJ 4yBjkHA7uH/iwuPcee5upCBoPj UACfXqYH
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
9099
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 it difficult to read/write an integer directly as a bit-stream to a file. However, I'm at a bit of a loss for how to do the following. So as not to...
7
728
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 can pass a max of 512 words to it at a time. So I would pass them in chunks of 512 words until the whole file has been processed. I haven't worked...
1
3465
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 value representing the width of the bmp image. So far, I have been able to use fstream's seek, write, and read to pull out chucks of bytes and...
7
1869
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 itself no problem... each record needs to be converted into std::vector<mystruct> I'm having a helluva time with the binary-->(pod)datatype conversion....
2
7797
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 to pack the structure to 1 byte boundries, and then just write out the structure directly to the File IO function pragma pack (1 typedef struct...
6
16011
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 vstr = rksub.GetValue(vs);
3
2617
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 leads to the second question. I got the "Conversion buffer overflow" error when I run the following code: Stream s = openFileDialog1.OpenFile();
26
4287
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 Internet Explorer, XPCOM/XPConnect with Mozilla/Firefox). I am writing client-side code that will generate binary data for producing a GIF file...
2
4357
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. The binary data is how they send numbers (they call it "Big Endian"). I only know at run time whether a byte is going to be text or binary (one of the...
0
7852
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8221
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6629
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.