Results 1 to 9 of 9

Thread: How to create format stings for hexdump

  1. #1
    Join Date
    Aug 2011
    Location
    usa
    Posts
    232

    Default How to create format stings for hexdump

    Hi, I wanted to create a format string for hexdump but I can't figure it out and the examples in the man page don't work, for instance:
    Code:
    hexdump -n100 -e "%06.6_ao "  12/1 "%3_u " "\t\t" "%_p " "\n"
    The out put is:
    Code:
    hexdump: "%06.6_ao ": Bad format
    I've also tried putting the whole format string into one set of quotes but it will not accept that either.

  2. #2
    josephkk NNTP User

    Default Re: How to create format stings for hexdump

    On Wed, 01 Oct 2014 17:26:02 GMT, ballsystemlord
    <ballsystemlord@no-mx.forums.opensuse.org> wrote:

    >
    >Hi, I wanted to create a format string for hexdump but I can't figure it
    >out and the examples in the man page don't work, for instance:
    >
    >Code:
    >--------------------
    >
    > hexdump -n100 -e "%06.6_ao " 12/1 "%3_u " "\t\t" "%_p " "\n"
    >
    >--------------------
    >
    >The out put is:
    >
    >Code:
    >--------------------
    >
    > hexdump: "%06.6_ao ": Bad format
    >
    >--------------------
    >
    >I've also tried putting the whole format string into one set of quotes
    >but it will not accept that either.


    It sure is a ***** when the man page is wrong. Just the same, see
    fprintf() for more format string info.

    ?-)


  3. #3

    Default Re: How to create format stings for hexdump

    Quotes are tricky. Play with these examples to get ya started:

    Code:
    hexdump -s1 -n4 -ve '/1 "%_ax) "' -e '/1 "%02X" "\n"' <<<ABCDEFGHIJK
    
    hexdump  -s96 -n4 -ve '"0x" 4/1 "%02x" "\n"' ${HOME}/.mozilla/firefox/aProfileName/places.sqlite

  4. #4
    Join Date
    Aug 2011
    Location
    usa
    Posts
    232

    Default Re: How to create format stings for hexdump

    Thanks, it works, but what does the /1 do?

  5. #5
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,996

    Default Re: How to create format stings for hexdump

    Quote Originally Posted by ballsystemlord View Post
    Hi, I wanted to create a format string for hexdump but I can't figure it out and the examples in the man page don't work, for instance:
    Code:
    hexdump -n100 -e "%06.6_ao "  12/1 "%3_u " "\t\t" "%_p " "\n"
    The out put is:
    Code:
    hexdump: "%06.6_ao ": Bad format
    I've also tried putting the whole format string into one set of quotes but it will not accept that either.
    The man page says:
    -e format_string
    Specify a format string to be used for displaying data.
    That means that following the -e argument, there must be one and only one format_string argument. You have six! That is because the shell uses white space to delimit arguments.
    Second thing is that the " that should be in your format_string are not escaped from the shell, thus they are removed before the arguments are created. So after the shell did what it should so you have
    Code:
    hexdump|-n100|-e|%06.6_ao |12/1|%3_u |\t\t|%_p |\n
    (where I used | to delimit arguments for readability) which is not what you want.
    Solution: escape the whole format_string:
    Code:
    henk@boven:~> hexdump -n100 -e "%06.6_ao "  12/1 "%3_u " "\t\t" "%_p " "\n"
    hexdump: ongeldige opmaak {%06.6_ao }
    henk@boven:~> hexdump -n100 -e '"%06.6_ao "  12/1 "%3_u " "\t\t" "%_p " "\n"'
    aap
    000000   a   a   p  lf                                           
    henk@boven:~>
    See the difference?
    Henk van Velden

  6. #6

    Default Re: How to create format stings for hexdump

    Quote Originally Posted by ballsystemlord View Post
    Thanks, it works, but what does the /1 do?
    'Repeat format / Number of bytes to process '

    Repeat format or iteration defaults to 1, so could be written 1/1.

    From man hexdump:
    The iteration count is an optional positive integer, which defaults to one. Each format is applied iteration count times.

    The byte count is an optional positive integer. If specified it defines the number of bytes to be interpreted by each iteration of the format.

    If an iteration count and/or a byte count is specified, a single slash must be placed after the iteration count and/or before the byte count to disambiguate them. Any whitespace before or after the slash is ignored.

  7. #7
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,996

    Default Re: How to create format stings for hexdump

    @ballsystemlord,

    I think that the hidden, but important message here is that one has to know more about the shell's way of interpreting the commands that are typed in. As soon as you type commands that are a bit more complex then just a program name with one or more simple file names, you should have at least basic knowledge about special to the shell characters, word splitting, quoting, etc.

    That is a rather steep learning curve, but that is how it is
    Henk van Velden

  8. #8
    Join Date
    Aug 2011
    Location
    usa
    Posts
    232

    Default Re: How to create format stings for hexdump

    Quote Originally Posted by hcvv View Post
    I think that the hidden, but important message here is that one has to know more about the shell's way of interpreting the commands that are typed in. As soon as you type commands that are a bit more complex then just a program name with one or more simple file names, you should have at least basic knowledge about special to the shell characters, word splitting, quoting, etc.
    Personally, if I were to give an example in a man page I'd quote it corretcly (or tell the user that they were responsible for quoting the example code), and not place a line break in the middle of an argument. As a result I thought that the double quotes were intended for the shell as for the line break and the extra arguments, I was totally confused.

  9. #9
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,996

    Default Re: How to create format stings for hexdump

    Quote Originally Posted by ballsystemlord View Post
    Personally, if I were to give an example in a man page I'd quote it corretcly (or tell the user that they were responsible for quoting the example code), and not place a line break in the middle of an argument. As a result I thought that the double quotes were intended for the shell as for the line break and the extra arguments, I was totally confused.
    I agree with you that at least a remark not to forget quoting according to the shell you use should be in the man page. It is often done. But do not forget that the writers of man pages assume that you do have intimate knowledge of the shell. And the apearence of ' and/or " and/or { and/or white space in examples, etc. should start the alarm bells ringing in your head.
    Henk van Velden

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •