Results 1 to 10 of 10

Thread: Python: hex string to char

  1. #1
    Join Date
    Feb 2006
    Location
    The Netherlands
    Beans
    209
    Distro
    Xubuntu 8.10 Intrepid Ibex

    Python: hex string to char

    In Python, I have the following string:
    Code:
    3C 4D 76 77 61 66
    and so on.
    These are ASCII characters and I'd like to output them as text.
    I know how to extract each pair from a string, but how do I convert for instance the string "3C" to a character (like chr(0x3C))?
    The mumak butts! The grid bug bites! You get zapped! -- More --
    The leocrotta kicks! You die... -- More --
    Do you want your possessions identified? (y/n)

  2. #2
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: Python: hex string to char

    Did youchecked builtinn functions: http://docs.python.org/lib/built-in-funcs.html ?

  3. #3
    Join Date
    Jan 2006
    Location
    Philadelphia
    Beans
    4,076
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Python: hex string to char

    the following python session may be of help:
    Code:
    >>> import binascii
    >>> binascii.unhexlify('3C 4D 76 77 61 66')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: Odd-length string
    >>> binascii.unhexlify(''.join('3C 4D 76 77 61 66'.split()))
    '<Mvwaf'
    >>>
    binascii.unhexlify() takes a hex string (no spaces between characters), and does exactly what you need.

  4. #4
    Join Date
    Feb 2006
    Location
    The Netherlands
    Beans
    209
    Distro
    Xubuntu 8.10 Intrepid Ibex

    Re: Python: hex string to char

    Quote Originally Posted by nanotube View Post
    binascii.unhexlify() takes a hex string (no spaces between characters), and does exactly what you need.
    Thanks, that was exactly what I needed!
    The mumak butts! The grid bug bites! You get zapped! -- More --
    The leocrotta kicks! You die... -- More --
    Do you want your possessions identified? (y/n)

  5. #5
    Join Date
    Jan 2006
    Location
    Philadelphia
    Beans
    4,076
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Python: hex string to char

    Quote Originally Posted by AZzKikR View Post
    Thanks, that was exactly what I needed!
    great. glad i could be of help

  6. #6
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Python: hex string to char

    Code:
    def hextranslate(s):
            res = ""
            for i in range(len(s)/2):
                    realIdx = i*2
                    res = res + chr(int(s[realIdx:realIdx+2],16))
            return res

  7. #7
    Join Date
    Jan 2006
    Location
    Philadelphia
    Beans
    4,076
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Python: hex string to char

    Quote Originally Posted by ghostdog74 View Post
    Code:
    def hextranslate(s):
            res = ""
            for i in range(len(s)/2):
                    realIdx = i*2
                    res = res + chr(int(s[realIdx:realIdx+2],16))
            return res
    nice. but you forgot to account for the spaces between the hex pairs (the format of the OP's string). gotta either take them out with a split/join, or s/2/3/ in your code.

  8. #8
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Python: hex string to char

    Quote Originally Posted by nanotube View Post
    nice. but you forgot to account for the spaces between the hex pairs
    its left as an exercise for OP. where's the fun in learning if I do everything ?

  9. #9
    Join Date
    Feb 2006
    Location
    The Netherlands
    Beans
    209
    Distro
    Xubuntu 8.10 Intrepid Ibex

    Re: Python: hex string to char

    Quote Originally Posted by ghostdog74 View Post
    Code:
    ...
        res = res + chr(int(s[realIdx:realIdx+2],16))
    ...
    Ah. The int(str, base) was what I was looking for as well. Java is my primary language, and it has a parsing mechanism from strings to integers using a base (10 for decimals, 16 for hex, 2 for binary). I was looking for something like that in Python, and I never knew int() could accept two arguments
    The mumak butts! The grid bug bites! You get zapped! -- More --
    The leocrotta kicks! You die... -- More --
    Do you want your possessions identified? (y/n)

  10. #10
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Python: hex string to char

    Quote Originally Posted by AZzKikR View Post
    and I never knew int() could accept two arguments
    for newer version of Python, yes.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •