Converting Hex to decimal

Discussion in 'Python' started by dean, Jun 1, 2004.

  1. dean

    dean Guest

    Hello group:

    I have a file containing hex data. I would like to convert the
    contents of the file to decimal by reading each by sequentially (one
    byte at the time). Is there a module that has such functionality or
    does someone have the relevant code?

    Also, is there an oracle module available for windows xp and hp-ux?

    Much appreciated

    dean

    please reply to diabolik(nospam)@uku.co.uk remove (nospam)
    dean, Jun 1, 2004
    #1
    1. Advertising

  2. dean

    Miki Tebeka Guest

    Hello Dean,

    > I have a file containing hex data. I would like to convert the
    > contents of the file to decimal by reading each by sequentially (one
    > byte at the time). Is there a module that has such functionality or
    > does someone have the relevant code?

    Have a look at `array' or `struct' modules.
    If you just want to read "raw" bytes you can do:
    for c in open("some_file").read():
    print hex(ord(c))
    (Note that this read *all* the file into memory)

    HTH.
    --
    -------------------------------------------------------------------------
    Miki Tebeka <>
    http://tebeka.web1000.com
    The only difference between children and adults is the price of the toys.
    Miki Tebeka, Jun 1, 2004
    #2
    1. Advertising

  3. dean

    Peter Hansen Guest

    dean wrote:

    > I have a file containing hex data.


    What does the term "hex data" mean to you? Does it mean
    binary? Does it mean that there are only characters in
    the range 0-9A-F and that each pair of characters represents
    a single byte? Something else? It is not a term with a
    precisely defined meaning.

    > I would like to convert the
    > contents of the file to decimal by reading each by sequentially (one
    > byte at the time). Is there a module that has such functionality or
    > does someone have the relevant code?


    Definitely Python can do it, possibly with the struct module,
    but it all depends on what "hex data" means. Do you have
    an example of what input will translate to what output?

    -Peter
    Peter Hansen, Jun 1, 2004
    #3
  4. dean

    fishboy Guest

    On 1 Jun 2004 01:51:23 -0700, (dean) wrote:

    >Hello group:
    >
    >I have a file containing hex data. I would like to convert the
    >contents of the file to decimal by reading each by sequentially (one
    >byte at the time). Is there a module that has such functionality or
    >does someone have the relevant code?
    >
    >Also, is there an oracle module available for windows xp and hp-ux?
    >
    >Much appreciated
    >
    >dean
    >
    >please reply to diabolik(nospam)@uku.co.uk remove (nospam)



    The hex data in file is a bit confusing. I can think of two ways you
    could mean this.

    For the one that is a file with text that reads like:

    2344
    fabd
    3343
    43ae
    fe34

    Hmm, how's about int()? It accepts a radix.

    >>> int('aefdcba',16)

    183491770
    >>> int('11',16)

    17

    The other way would be with struct/array like others have mentioned.

    ><{{{*>
    fishboy, Jun 1, 2004
    #4
  5. dean

    dean Guest

    Hello Group

    Thanks for your responses so far. I have not been clear it seems so
    here it goes:

    The file is binary. Using the standard hpux or linux hexdump (xxd or
    xd) this is the output that is obtained:

    0000000: 5c1a 0100 0100 ff35 0399 7272 9903 0002 \......5..rr....
    0000010: 010e 0300 050a 0214 ffff ffff 5cd4 2e01 ............\...
    0000020: fbb9 5900 b400 0000 0020 2000 b502 5423 ..Y...... ...T#
    0000030: 3a71 2e11 00ff ffff ffff ffff ffff ffff :q..............
    0000040: ffff ffff ff00 ff07 0209 3519 4fff ffff ..........5.O...
    0000050: ffff ffff ffff ffff ffff ffff ffff ffff ................
    0000060: ffff ffff ffff ffff ffff ffff ffff ffff ................
    0000070: ffff ffff ffff ffff ffff ffff ffff ffff ................
    0000080: ffff ffff ffff ff00 5435 3617 040a d207 ........T56.....
    0000090: 3e05 3717 040a d207 0000 0000 2c01 6000 >.7.........,.`.
    00000a0: 0141 01a8 0005 030b 0300 00ff ffff ffff .A..............
    00000b0: ffff ffff ffff ffff ffff ff00 0000 0000 ................
    00000c0: 0300 0000 5435 3617 040a d207 0b00 0000 ....T56.........
    00000d0: 0000 0000 fcb9 5900 b400 0000 0020 2000 ......Y...... .
    00000e0: b602 5423 3b71 2e11 00ff ffff ffff ffff ..T#;q..........
    00000f0: ffff ffff ffff ffff ff00 2094 3965 0702 .......... .9e..
    0000100: 0935 194f ffff ffff ffff ff07 0209 3519 .5.O..........5.
    0000110: 4fff ffff ffff ffff ffff ffff ffff ffff O...............
    0000120: ffff ffff ffff ffff ffff ffff ffff ffff ................
    0000130: ffff ffff ffff ffff ffff ff00 1536 3617 .............66.

    Each hex character is 8 bits. I would like to convert one byte at the
    time and pipe the converted data into a new file.

    Thanx in advance
    Regards
    Dean
    dean, Jun 2, 2004
    #5
  6. dean wrote:
    > The file is binary. Using the standard hpux or linux hexdump (xxd or
    > xd) this is the output that is obtained:
    >
    > 0000000: 5c1a 0100 0100 ff35 0399 7272 9903 0002 \......5..rr....
    > 0000010: 010e 0300 050a 0214 ffff ffff 5cd4 2e01 ............\...
    > 0000020: fbb9 5900 b400 0000 0020 2000 b502 5423 ..Y...... ...T#
    > 0000030: 3a71 2e11 00ff ffff ffff ffff ffff ffff :q..............
    > 0000040: ffff ffff ff00 ff07 0209 3519 4fff ffff ..........5.O...
    > 0000050: ffff ffff ffff ffff ffff ffff ffff ffff ................
    > 0000060: ffff ffff ffff ffff ffff ffff ffff ffff ................
    > 0000070: ffff ffff ffff ffff ffff ffff ffff ffff ................
    > 0000080: ffff ffff ffff ff00 5435 3617 040a d207 ........T56.....
    > 0000090: 3e05 3717 040a d207 0000 0000 2c01 6000 >.7.........,.`.
    > 00000a0: 0141 01a8 0005 030b 0300 00ff ffff ffff .A..............
    > 00000b0: ffff ffff ffff ffff ffff ff00 0000 0000 ................
    > 00000c0: 0300 0000 5435 3617 040a d207 0b00 0000 ....T56.........
    > 00000d0: 0000 0000 fcb9 5900 b400 0000 0020 2000 ......Y...... .
    > 00000e0: b602 5423 3b71 2e11 00ff ffff ffff ffff ..T#;q..........
    > 00000f0: ffff ffff ffff ffff ff00 2094 3965 0702 .......... .9e..
    > 0000100: 0935 194f ffff ffff ffff ff07 0209 3519 .5.O..........5.
    > 0000110: 4fff ffff ffff ffff ffff ffff ffff ffff O...............
    > 0000120: ffff ffff ffff ffff ffff ffff ffff ffff ................
    > 0000130: ffff ffff ffff ffff ffff ff00 1536 3617 .............66.
    >
    > Each hex character is 8 bits. I would like to convert one byte at the
    > time and pipe the converted data into a new file.


    Ok, you've got the hexdump and, as far as I understand,
    you want the binary data back, byte by byte.

    import binascii

    def makeBinary (f):
    for line in f:
    words = line.split () [1:9]
    for word in words:
    yield binascii.unhexlify (word [:2])
    yield binascii.unhexlify (word [2:])

    Then use the generator this way:

    for byte in makeBinary (open ("hexdumpfile", "r")):
    [...]

    I did not test it, but at least it is an outline of
    the way it can be done.

    Greetings,

    Holger
    =?ISO-8859-1?Q?Holger_T=FCrk?=, Jun 2, 2004
    #6
  7. dean

    Miki Tebeka Guest

    Hello Dean,

    > Each hex character is 8 bits. I would like to convert one byte at the
    > time and pipe the converted data into a new file.

    This is not tested but should be in the lines of what you're trying to
    do:

    from array import array
    a = array("B")
    a.fromstring(open("input_file", "rb").read()) # Read all file
    out = open("output_file", "wb")
    for bit in a:
    new_bit = convert(bit)
    out.write(new_bit)


    HTH.
    --
    -------------------------------------------------------------------------
    Miki Tebeka <>
    http://tebeka.web1000.com
    The only difference between children and adults is the price of the toys.
    Miki Tebeka, Jun 2, 2004
    #7
  8. dean

    Peter Hansen Guest

    dean wrote:

    > Thanks for your responses so far. I have not been clear it seems so
    > here it goes:


    Judging by the subsequent responses, you're still not being
    clear. :)

    > The file is binary.


    In other words, it is *not* "hex". It's binary. Plain and simple.
    Is that true?

    > Using the standard hpux or linux hexdump (xxd or
    > xd) this is the output that is obtained:


    So the only hex thing about it is that you can display the contents
    using a program that displays binary content as hex?

    > 0000000: 5c1a 0100 0100 ff35 0399 7272 9903 0002 \......5..rr....
    > 0000010: 010e 0300 050a 0214 ffff ffff 5cd4 2e01 ............\...


    But the actual file that you have is the original, binary
    file, right? Not just this hexdump output?

    > Each hex character is 8 bits.


    Do you mean that each byte is 8 bits?

    > I would like to convert one byte at the
    > time and pipe the converted data into a new file.


    Convert in what way? Or do you really mean that you have only
    the hexdump output and want to convert it back to the raw binary
    file from which it was originally generated, as the last several
    respondents have assumed?

    Otherwise, it really sounds like you just want to read the file
    byte-by-byte and "convert" the bytes in some way, writing them
    to a new file. There is nothing special about doing this: you
    would just use open() and .read() and .write() like with any
    other file.

    -Peter
    Peter Hansen, Jun 2, 2004
    #8
    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,137
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    449
    Juha Autero
    Aug 19, 2003
  3. Earl Eiland

    Trouble converting hex to decimal?

    Earl Eiland, Feb 5, 2005, in forum: Python
    Replies:
    3
    Views:
    379
    Jorgen Grahn
    Feb 5, 2005
  4. Pedro Werneck

    Re: Trouble converting hex to decimal?

    Pedro Werneck, Feb 5, 2005, in forum: Python
    Replies:
    2
    Views:
    4,749
    Adam DePrince
    Feb 6, 2005
  5. Anders Bystrup

    Converting from decimal to hex

    Anders Bystrup, Jul 30, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    343
    John J. Trammell
    Jul 30, 2003
Loading...

Share This Page