hex dump?

S

SpreadTooThin

I have a program that receives binary data over udp.
I want to print out the recieved buffer as a bunch of hex digits...
eg: FF FE AB 22 DD

Right now I am doing:

System.out.println(receivePacket.getData())
I should have 102 bytes in the received packet...
 
J

Joshua Cranmer

SpreadTooThin said:
I have a program that receives binary data over udp.
I want to print out the recieved buffer as a bunch of hex digits...
eg: FF FE AB 22 DD

To answer your question, answer the following:
1. In what form is the data of a packet returned? A stream, a buffer, an
array... ?
2. How do you iterate over individual bytes of that format?
3. Given an individual byte, which could also be represented as an
integer, how would you turn it into a hexadecimal string.
System.out.println(receivePacket.getData())

Well, it is doing exactly what you told it to do. It's just that an
array's toString() method is the same as Object's.
 
S

SpreadTooThin

To answer your question, answer the following:
1. In what form is the data of a packet returned? A stream, a buffer, an
array... ?

I know I can make a string out getData... Does that help?
2. How do you iterate over individual bytes of that format?

for (i=0; i < 102; ++i)
and then index to the character in the String...
3. Given an individual byte, which could also be represented as an
integer, how would you turn it into a hexadecimal string.

This is where I am lost.. Its a char char.ToHexString?
 
A

Arne Vajhøj

SpreadTooThin said:
This is where I am lost.. Its a char char.ToHexString?

As Joshua wrote: a char can be converted to an int, so try
check the Integer class.

Arne
 
S

SpreadTooThin

As Joshua wrote: a char can be converted to an int, so try

check the Integer class.

Arne

Pardon this ignorant c/c++ programmer trying to do something in
Java...


char buffer[102];
int i;
for (i=0; i < 102; ++i)
printf("%02X ", buffer);

cmon.. how hard can it be do something like this in java from a
String?
 
J

Joshua Cranmer

SpreadTooThin said:
I know I can make a string out getData... Does that help?

getData() returns what, exactly?
for (i=0; i < 102; ++i)
and then index to the character in the String...

What if the packet isn't 102 bytes long?
This is where I am lost.. Its a char char.ToHexString?

Well, you can convert between all primitive types except boolean. I
hinted that you should treat it as an integer... where would you look
for converting integers into strings or vice versa?
 
J

Joshua Cranmer

SpreadTooThin said:
Pardon this ignorant c/c++ programmer trying to do something in
Java...


char buffer[102];
int i;
for (i=0; i < 102; ++i)
printf("%02X ", buffer);

cmon.. how hard can it be do something like this in java from a
String?


Numerous mistakes:
1. That's not how arrays are declared in Java.
2. A Java char is probably closer to the wchar_t type (for gcc, you'd
need -fshort-wchar, I think)
3. Hard-coding lengths is A Bad Thing™.
4. char[] != String. They are two completely distinct things.
 
J

John B. Matthews

SpreadTooThin said:
As Joshua wrote: a char can be converted to an int, so try

check the Integer class.

Arne

Pardon this ignorant c/c++ programmer trying to do something in
Java...

char buffer[102];
int i;
for (i=0; i < 102; ++i)
printf("%02X ", buffer);

cmon.. how hard can it be do something like this in java from a
String?


You may enjoy perusing this chrestomathy:

<http://c2.com/cgi/wiki?HexDumpInManyProgrammingLanguages>

My favorite:

#!/usr/bin/hexdump -f
"%06.6_ax: " 16/1 "%02x " " "
16/1 "%_p" "\n"
 
S

SpreadTooThin

Pardon this ignorant c/c++ programmer trying to do something in
Java...
char buffer[102];
int i;
for (i=0; i < 102; ++i)
   printf("%02X ", buffer);

cmon.. how hard can it be do something like this in java from a
String?

You may enjoy perusing this chrestomathy:

<http://c2.com/cgi/wiki?HexDumpInManyProgrammingLanguages>

My favorite:

#!/usr/bin/hexdump -f
"%06.6_ax: " 16/1 "%02x " "  "
16/1 "%_p" "\n"


Thanks for the link.. Very useful....

So this is what i did but I have one trouble and that is with 8 bit
values with bit 7 set... I prints out like a 32 bit..


int i, j, k;
i=0;
for (j=0; j < 17; ++j)
{
for (k=0; k < 6; ++k)
{
String hexString = Integer.toHexString(bytes);
++i;
System.out.print((hexString.length() < 2 ? "0" : "") + hexString
+ " ");
}
System.out.println("");
}

I think the issue is that Integer isn't a byte... it like 4 bytes..
I need byte.toHexString...
otherwise 0xFF get printed out like 0XFFFFFFFF
Know what I mean?
 
L

Lew

SpreadTooThin said:
SpreadTooThin said:
SpreadTooThin wrote:
3. Given an individual byte, which could also be represented as an
integer, how would you turn it into a hexadecimal string.
This is where I am lost.. Its a char char.ToHexString?
As Joshua wrote: a char can be converted to an int, so try
check the Integer class.
Arne
Pardon this ignorant c/c++ programmer trying to do something in
Java...
char buffer[102];
int i;
for (i=0; i < 102; ++i)
printf("%02X ", buffer);
cmon.. how hard can it be do something like this in java from a
String?

You may enjoy perusing this chrestomathy:

<http://c2.com/cgi/wiki?HexDumpInManyProgrammingLanguages>

My favorite:

#!/usr/bin/hexdump -f
"%06.6_ax: " 16/1 "%02x " " "
16/1 "%_p" "\n"


Thanks for the link.. Very useful....

So this is what i did but I have one trouble and that is with 8 bit
values with bit 7 set... I prints out like a 32 bit..


int i, j, k;
i=0;
for (j=0; j < 17; ++j)
{
for (k=0; k < 6; ++k)
{
String hexString = Integer.toHexString(bytes);
++i;
System.out.print((hexString.length() < 2 ? "0" : "") + hexString
+ " ");
}
System.out.println("");
}

I think the issue is that Integer isn't a byte... it like 4 bytes..
I need byte.toHexString...
otherwise 0xFF get printed out like 0XFFFFFFFF


The 'Integer.toHexString()' argument gets promoted to integer, and bytes are
signed. If you "and" the byte argument with 0xFF you should get what you want.
 
M

Mark Space

SpreadTooThin said:
char buffer[102];
int i;
for (i=0; i < 102; ++i)
printf("%02X ", buffer);

cmon.. how hard can it be do something like this in java from a
String?


String s = ....
for( int i = 0; i < s.length(); i++ ) {
System.out.printf( "%02X ", (int)s.charAt( i ) );
}
 
M

Mark Space

SpreadTooThin said:
String hexString = Integer.toHexString(bytes);
System.out.print((hexString.length() < 2 ? "0" : "") + hexString
+ " ");


I didn't look at the rest of the code, but Java supports a printf() call

System.out.printf( "%02X ", (int)byte[i++] );

might trim three lines into one for you.
 
M

Mark Space

John said:

Interesting, but:

private String bytesText(int perLine) {
String result = "";
for(int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes);
result += leftPadded(bytes < 0 ? "FF" : hex, 2) + " ";
}
while (perLine-- > bytes.length) result += " ";
return result;
}

Ouch, lot's of string concatenation. What about using a StringBuilder?
Constructs like this make me doubt the usefulness of the whole page.
 
J

Jarrick Chagma

Joshua Cranmer said:
SpreadTooThin said:
Pardon this ignorant c/c++ programmer trying to do something in
Java...


char buffer[102];
int i;
for (i=0; i < 102; ++i)
printf("%02X ", buffer);

cmon.. how hard can it be do something like this in java from a
String?


Numerous mistakes:
1. That's not how arrays are declared in Java.
2. A Java char is probably closer to the wchar_t type (for gcc, you'd
need -fshort-wchar, I think)
3. Hard-coding lengths is A Bad Thing™.
4. char[] != String. They are two completely distinct things.


I think the OP was showing you the equivalent C code that they wish to
translate into Java.
 
K

Knute Johnson

SpreadTooThin said:
Pardon this ignorant c/c++ programmer trying to do something in
Java...


char buffer[102];
int i;
for (i=0; i < 102; ++i)
printf("%02X ", buffer);

cmon.. how hard can it be do something like this in java from a
String?


I don't know why they are giving you a bad time but you pretty much have
the answer. It looks like you are receiving a UDP datagram. That will
be in a byte[] buffer and you print it out just like you expect. You
don't need to do any conversions, just print out the byte data with printf.

public class test {
public static void main(String[] args) {
byte[] buf = { (byte)0xff, 0x15, 0x2a, (byte)0x8f, 0x00 };

for (int i=0; i<buf.length; i++)
System.out.printf("%02X ",buf);
}
}
 
C

coffeymex

Pardon this ignorant c/c++ programmer trying to do something in
Java...

char buffer[102];
int i;
for (i=0; i < 102; ++i)
   printf("%02X ", buffer);

cmon.. how hard can it be do something like this in java from a
String?


You can do something very similar-- see the String.format() method. I
usually use something like the following to give me both the hex and
ASCII side by side:

public static void dumpHex(byte[] b) {
dumpHex(b, 0, b.length);
}
public static void dumpHex(byte[] b, int start, int len) {
int max = start + len;
for (int i = start; i < max; i += 16) {
StringBuilder sb = new StringBuilder(64);
int xmax = Math.min(16, max - i);
for (int j = 0; j < xmax; j++) {
int bt = b[i+j] & 0xff;
sb.append(String.format("%02X", bt));
}
for (int noSpaces = 16-xmax; noSpaces >= 0; noSpaces--) {
sb.append(" ");
}
for (int j = 0; j < xmax; j++) {
int bt = b[i+j] & 0xff;
char ch = (bt >= 32 && (bt < 256 && bt != 127)) ? ((char)
bt) : '.';
sb.append(ch);
}
System.out.println(sb);
}
}
 
R

Roedy Green

I have a program that receives binary data over udp.
I want to print out the recieved buffer as a bunch of hex digits...
eg: FF FE AB 22 DD

Right now I am doing:

see http://mindprod.com/jgloss/hex.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Species evolve exactly as if they were adapting as best they could to a changing world, and not at all as if they were moving toward a set goal."
~ George Gaylord Simpson
 
L

Lew

Ian said:
I would use:

for( int i = 0, l = s.length(); i < l; i++ ) {

similar argument to the stringbuilder [sic] one

'l' is a terrible choice for a variable name. I thought you were comparing
'i' to '1'.

The value of this micro-optimization is dubious, but I admit to using it myself.

I am not familiar with 'stringbuilder'. Custom class? :)
 
T

Thomas Kellerer

Ian Smith, 11.05.2009 10:34:
for( int i = 0; i < s.length(); i++ ) {
vs.

for( int i = 0, l = s.length(); i < l; i++ ) {

I would expect the compiler to see and optimize that automatically.

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top