Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Converting Hex to decimal

Reply
Thread Tools

Converting Hex to decimal

 
 
dean
Guest
Posts: n/a
 
      06-01-2004
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)
 
Reply With Quote
 
 
 
 
Miki Tebeka
Guest
Posts: n/a
 
      06-01-2004
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.

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      06-01-2004
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
 
Reply With Quote
 
fishboy
Guest
Posts: n/a
 
      06-01-2004
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.

><{{{*>


 
Reply With Quote
 
dean
Guest
Posts: n/a
 
      06-02-2004
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
 
Reply With Quote
 
=?ISO-8859-1?Q?Holger_T=FCrk?=
Guest
Posts: n/a
 
      06-02-2004


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

 
Reply With Quote
 
Miki Tebeka
Guest
Posts: n/a
 
      06-02-2004
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.

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      06-02-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
Re: Trouble converting hex to decimal? Pedro Werneck Python 2 02-06-2005 07:33 AM
Trouble converting hex to decimal? Earl Eiland Python 3 02-05-2005 05:53 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM
Converting from decimal to hex Anders Bystrup Perl Misc 1 07-30-2003 03:14 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57