String to hexadecimal conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Praveena P

    String to hexadecimal conversion

    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 : 00000000000020E 0000032F8000000 00400022005E
    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
  • Praveena P

    #2
    Re: String to hexadecimal conversion

    On Sep 8, 2:05 pm, Praveena P <praveenapa...@ gmail.comwrote:
    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 : 00000000000020E 0000032F8000000 00400022005E
    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?

    Comment

    • John Machin

      #3
      Re: String to hexadecimal conversion

      On Sep 8, 7:05 pm, Praveena P <praveenapa...@ gmail.comwrote:
      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 : 00000000000020E 0000032F8000000 00400022005E
      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?
      *IF* all the data consists of unsigned 8-bit integers
      a_byte = f.read(2)
      uint8 = int(a_byte, 16)

      But I doubt it and that would be rather slow anyway. If your data is
      homogenous, look at the array module. Otherwise, once you've worked
      out how to break your file down into records, and what the layout of
      each record is, you'll need the struct module.

      HTH,
      John

      Comment

      • Praveena P

        #4
        Re: String to hexadecimal conversion

        On Sep 8, 2:31 pm, John Machin <sjmac...@lexic on.netwrote:
        On Sep 8, 7:05 pm, Praveena P <praveenapa...@ gmail.comwrote:
        >
        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 : 00000000000020E 0000032F8000000 00400022005E
        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?
        >
        *IF* all the data consists of unsigned 8-bit integers
        a_byte = f.read(2)
        uint8 = int(a_byte, 16)
        >
        But I doubt it and that would be rather slow anyway. If your data is
        homogenous, look at the array module. Otherwise, once you've worked
        out how to break your file down into records, and what the layout of
        each record is, you'll need the struct module.
        >
        HTH,
        John
        Hey John,

        Thanks! That was useful...
        I am still putting the code together with all the operations... will
        probably have loads more queries... :-)

        Have a good day!
        Praveena

        Comment

        • Steve Holden

          #5
          Re: String to hexadecimal conversion

          Praveena P 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 : 00000000000020E 0000032F8000000 00400022005E
          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?
          Generally speaking, reading a file 2 bytes at a time i going to be
          inefficient and untidy.

          Does this help?
          >>int("00000000 000020E0000032F 800000000400022 005E", 16)
          170696759285949 896156423472451 551326L
          >>>
          Or have I misunderstood your intention?

          regards
          Steve
          --
          Steve Holden +1 571 484 6266 +1 800 494 3119
          Holden Web LLC http://www.holdenweb.com/

          Comment

          • imageguy

            #6
            Re: String to hexadecimal conversion

            On Sep 8, 5:05 am, Praveena P <praveenapa...@ gmail.comwrote:
            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 : 00000000000020E 0000032F8000000 00400022005E
            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
            check out the "struct" module in the standard python library.
            It's title says it all "Interpreti ng strings as packed binary data".
            I used this extensively to reverse engineer a couple proprietary file
            structures.

            You can read large chucks of the file - perhaps a record ? - and then
            based on the format provided, convert it to a tuple.

            Good luck.

            g.

            Comment

            Working...