String to hexadecimal conversion

Praveena P praveenapanch at gmail.com
Mon Sep 8 05:26:20 EDT 2008


On Sep 8, 2:05 pm, Praveena P <praveenapa... at gmail.com> wrote:
> Hi folks,
>
> I am new to Python... so am not too sure about how the type conversion
> works.
>
> I have to read a file that contains hexadecimal data and use the data
> further to do some arithmetic calculations.
> A sample of the input is : 00000000000020E0000032F800000000400022005E
> The problem I am facing is this:
> I am using f.read(2) to read a byte at a time, but the data that is
> read is a string rather than a number. So it kind of hampers any
> arithmetic operations I perform on this data...
>
> Could you please suggest some method I could use for this?
>
> Thanks guys!
> Praveena

I was thinking of using dictionary like this:
hex_to_decimal = {"1":"1", "2":"2", "3":"3", "4":"4", "5":"5",
"6":"6", "7":"7", "8":"8", "9":"9", "A":"10", "B":"11", "C":"12",
"D":"13", "E":"14", "F":"15", "0":"0"}

Extracting one character at a time and converting to decimal, and then
using the decimal numbers generated for the further calculations....

Would there be a simpler way to do it?



More information about the Python-list mailing list