convert assembly data (double words and bytes) in two tupple

Discussion in 'Python' started by joblack, Dec 8, 2010.

  1. joblack

    joblack Guest

    I have two assembly data txt files, one has the structure:

    0E1459D1Fh, 0AB58FAAEh, 4303E35Bh, 55FA3020h, 0E66D76ADh,
    0EF434544h, ...

    and the other has the structure:

    53h, 6, 6Bh, 0D4h, 40h, 35h, 0B5h, 33h, 0AFh, 30h, 0B3h,
    66h, ...

    (I removed the dd and db with awk)

    Now I want both of them in a tupple (like map1 = ( ... ) and map2 =
    ( ...) in my source code so I can index them..

    Any idea how to give python a hint that the h at the end of the number
    means it's a hex number? Or do I have to torture myself with regex to
    get it right?
     
    joblack, Dec 8, 2010
    #1
    1. Advertising

  2. joblack

    Chris Rebert Guest

    On Tue, Dec 7, 2010 at 6:36 PM, joblack <> wrote:
    > I have two assembly data txt files, one has the structure:
    >
    > 0E1459D1Fh, 0AB58FAAEh, 4303E35Bh, 55FA3020h, 0E66D76ADh,
    > 0EF434544h, ...
    >
    > and the other has the structure:
    >
    > 53h, 6, 6Bh, 0D4h, 40h, 35h, 0B5h, 33h, 0AFh, 30h, 0B3h,
    > 66h, ...
    >
    > (I removed the dd and db with awk)
    >
    > Now I want both of them in a tupple (like map1 = ( ... ) and map2 =
    > ( ...) in my source code so I can index them..
    >
    > Any idea how to give python a hint that the h at the end of the number
    > means it's a hex number? Or do I have to torture myself with regex to
    > get it right?


    No regex necessary whatsoever:

    # will be inefficient if file is huge
    f = open("path/to/assembly/file/1")
    content = f.read()
    f.close()
    map1 = [int(hexnum[:-1], 16) for hexnum content.split(", ")]
    # do same thing for 2nd file

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Dec 8, 2010
    #2
    1. Advertising

  3. joblack

    MRAB Guest

    On 08/12/2010 02:36, joblack wrote:
    > I have two assembly data txt files, one has the structure:
    >
    > 0E1459D1Fh, 0AB58FAAEh, 4303E35Bh, 55FA3020h, 0E66D76ADh,
    > 0EF434544h, ...
    >
    > and the other has the structure:
    >
    > 53h, 6, 6Bh, 0D4h, 40h, 35h, 0B5h, 33h, 0AFh, 30h, 0B3h,
    > 66h, ...
    >
    > (I removed the dd and db with awk)
    >
    > Now I want both of them in a tupple (like map1 = ( ... ) and map2 =
    > ( ...) in my source code so I can index them..
    >
    > Any idea how to give python a hint that the h at the end of the number
    > means it's a hex number? Or do I have to torture myself with regex to
    > get it right?


    If you insist on not using regex :)-)) then:

    >>> text = "53h, 6, 6Bh, 0D4h, 40h, 35h, 0B5h, 33h, 0AFh, 30h, 0B3h, 66h"
    >>> data = [t.strip() for t in text.split(",")]
    >>> data

    ['53h', '6', '6Bh', '0D4h', '40h', '35h', '0B5h', '33h', '0AFh', '30h',
    '0B3h', '66h']
    >>> data = [int(d[ : -1], 16) if d[-1] == "h" else int(d) for d in data]
    >>> data

    [83, 6, 107, 212, 64, 53, 181, 51, 175, 48, 179, 102]
     
    MRAB, Dec 8, 2010
    #3
    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. da Vinci
    Replies:
    5
    Views:
    8,751
    da Vinci
    Jul 31, 2004
  2. Sydex
    Replies:
    12
    Views:
    6,575
    Victor Bazarov
    Feb 17, 2005
  3. Heiko Vogel
    Replies:
    3
    Views:
    577
    Method Man
    Sep 14, 2004
  4. Stef Mientki
    Replies:
    0
    Views:
    286
    Stef Mientki
    Aug 15, 2009
  5. Stef Mientki
    Replies:
    0
    Views:
    1,101
    Stef Mientki
    Aug 15, 2009
Loading...

Share This Page