hex dump?

Discussion in 'Java' started by SpreadTooThin, May 11, 2009.

  1. 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...
    SpreadTooThin, May 11, 2009
    #1
    1. Advertising

  2. SpreadTooThin wrote:
    > 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.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, May 11, 2009
    #2
    1. Advertising

  3. On May 10, 8:12 pm, Joshua Cranmer <> wrote:
    > SpreadTooThin wrote:
    > > 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... ?


    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?

    >
    > > 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.
    >
    > --
    > Beware of bugs in the above code; I have only proved it correct, not
    > tried it. -- Donald E. Knuth
    SpreadTooThin, May 11, 2009
    #3
  4. SpreadTooThin

    Arne Vajhøj Guest

    SpreadTooThin wrote:
    > On May 10, 8:12 pm, Joshua Cranmer <> 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
    Arne Vajhøj, May 11, 2009
    #4
  5. On May 10, 8:39 pm, Arne Vajhøj <> wrote:
    > SpreadTooThin wrote:
    > > On May 10, 8:12 pm, Joshua Cranmer <> 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?
    SpreadTooThin, May 11, 2009
    #5
  6. SpreadTooThin

    Lew Guest

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


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


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


    Hey, Arne told you a pretty potent hint.
    <http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#toHexString(int)>

    --
    Lew
    Lew, May 11, 2009
    #6
  7. SpreadTooThin wrote:
    > On May 10, 8:12 pm, Joshua Cranmer <> wrote:
    >> SpreadTooThin wrote:
    >>> 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... ?

    >
    > I know I can make a string out getData... Does that help?


    getData() returns what, exactly?

    >> 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...


    What if the packet isn't 102 bytes long?

    >> 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?


    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?

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, May 11, 2009
    #7
  8. SpreadTooThin wrote:
    > 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.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, May 11, 2009
    #8
  9. In article
    <>,
    SpreadTooThin <> wrote:

    > On May 10, 8:39 pm, Arne Vajhøj <> wrote:
    > > SpreadTooThin wrote:
    > > > On May 10, 8:12 pm, Joshua Cranmer <> 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"

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, May 11, 2009
    #9
  10. On May 10, 9:13 pm, "John B. Matthews" <> wrote:
    > In article
    > <>,
    >
    >
    >
    >  SpreadTooThin <> wrote:
    > > On May 10, 8:39 pm, Arne Vajhøj <> wrote:
    > > > SpreadTooThin wrote:
    > > > > On May 10, 8:12 pm, Joshua Cranmer <> 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"
    >
    > --
    > John B. Matthews
    > trashgod at gmail dot com
    > <http://sites.google.com/site/drjohnbmatthews>


    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?
    SpreadTooThin, May 11, 2009
    #10
  11. SpreadTooThin

    Lew Guest

    SpreadTooThin wrote:
    > On May 10, 9:13 pm, "John B. Matthews" <> wrote:
    >> In article
    >> <>,
    >>
    >>
    >>
    >> SpreadTooThin <> wrote:
    >>> On May 10, 8:39 pm, Arne Vajhøj <> wrote:
    >>>> SpreadTooThin wrote:
    >>>>> On May 10, 8:12 pm, Joshua Cranmer <> 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"
    >>
    >> --
    >> John B. Matthews
    >> trashgod at gmail dot com
    >> <http://sites.google.com/site/drjohnbmatthews>

    >
    > 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.

    --
    Lew
    Lew, May 11, 2009
    #11
  12. SpreadTooThin

    Mark Space Guest

    SpreadTooThin wrote:

    > 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 ) );
    }
    Mark Space, May 11, 2009
    #12
  13. SpreadTooThin

    Mark Space Guest

    SpreadTooThin wrote:

    > 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.
    Mark Space, May 11, 2009
    #13
  14. SpreadTooThin

    Mark Space Guest

    John B. Matthews wrote:

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


    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.
    Mark Space, May 11, 2009
    #14
  15. "Joshua Cranmer" <> wrote in message
    news:gu84re$q5a$...
    > SpreadTooThin wrote:
    >> 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.

    --
    Regards,

    Jarrick

    ------------------------------------

    (Lose your mind before emailing me)
    Jarrick Chagma, May 11, 2009
    #15
  16. SpreadTooThin wrote:
    > 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);
    }
    }

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, May 11, 2009
    #16
  17. SpreadTooThin

    coffeymex Guest

    On May 10, 9:47 pm, SpreadTooThin <> wrote:

    > 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);
    }
    }
    coffeymex, May 11, 2009
    #17
  18. SpreadTooThin

    Roedy Green Guest

    On Sun, 10 May 2009 18:52:15 -0700 (PDT), SpreadTooThin
    <> wrote, quoted or indirectly quoted someone who
    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
    >
    >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
    Roedy Green, May 11, 2009
    #18
  19. SpreadTooThin

    Lew Guest

    Ian Smith wrote:
    > 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? :)

    --
    Lew
    Lew, May 11, 2009
    #19
  20. 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
    Thomas Kellerer, May 11, 2009
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,191
    Neredbojias
    Aug 19, 2005
  2. halfdog
    Replies:
    12
    Views:
    12,444
  3. Matt
    Replies:
    4
    Views:
    3,421
    Socket
    Jul 29, 2004
  4. Bengt Richter
    Replies:
    6
    Views:
    467
    Juha Autero
    Aug 19, 2003
  5. Replies:
    7
    Views:
    988
    Mike Wahler
    Jul 1, 2007
Loading...

Share This Page