#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    2
    Rep Power
    0

    convert binary to hex


    I would like to read some binary numbers from a file and convert them to hexadecimal numbers and then write those hexadecimal numbers to another file. Can anyone help with a C script that would do this?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Inside a computer, all numeric value are in binary. Please be more explicit in describing what you want to do.

    My assumption of what you are asking: you want to --
    1. Read values from a binary (ie, non-text) file
    2. Output them those values to a text file in hexadecimal format.

    Using fprintf to write to the output text file, use the %X or %x format specifier.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    55
    Rep Power
    11
    It needs to be %02x per byte read, otherwise you may lose nibbles which are 0.
  6. #4
  7. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    11
    you can fscanf("%d/* decimal*/") numbers and then fprintf("%x/*hexadecimal*/") numbers.

    Fascinating. :)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    55
    Rep Power
    11
    why would he fscanf to an integer location when he can just read (or map) the file into memory & go through the data a lot faster (assuming the file is not HUGE)? And as I said before, %02x, not just %x; if reading 1 byte at a time, reading a nibble is ridiculous.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally posted by xtor
    why would he fscanf to an integer location when he can just read (or map) the file into memory & go through the data a lot faster (assuming the file is not HUGE)? And as I said before, %02x, not just %x; if reading 1 byte at a time, reading a nibble is ridiculous.
    Or %04X for 16-bit unsigned ints or %08X for unsigned longs. The thing is that we don't know whether he wants to display them byte-wise, as in a hex dump, or as short or long ints.

    BTW, there's a real nice UNIX/Linux utility, xxd, which generates a hex dump of whatever you feed it. It also comes in a Windows port. Maybe that will do what he wants.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    55
    Rep Power
    11
    Why just unsigned? Unsigned are almost always the same size as signed (in bits).

    I thought my explanation was plenty, it hinted a single hexadecimal number represents 1 nibble, from that it's pretty easy to work out how many preceeding zero's should be used for each data size.
    Last edited by xtor; August 18th, 2003 at 06:13 PM.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally posted by xtor
    Why just unsigned? Unsigned are almost always the same size as signed (in bits).

    I thought my explanation was plenty, it hinted a single hexadecimal number represents 1 nibble, from that it's pretty easy to work out how many preceeding zero's should be used for each data size.
    According to my reference, by definition, the %x format specifier is unsigned.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    55
    Rep Power
    11
    man pages state both %x and %X take unsigned integers (the %C outputs hexadecimal characters in capitals); although the hexadecimal output is sign independant, strange.

IMN logo majestic logo threadwatch logo seochat tools logo