Search code examples
pythonhexrepresentation

Converting a hex-string representation to actual bytes in Python


i need to load the third column of this text file as a hex string

http://www.netmite.com/android/mydroid/1.6/external/skia/emoji/gmojiraw.txt

>>> open('gmojiraw.txt').read().split('\n')[0].split('\t')[2]
'\\xF3\\xBE\\x80\\x80'

how do i open the file so that i can get the third column as hex string:

'\xF3\xBE\x80\x80'

i also tried binary mode and hex mode, with no success.


Solution

  • You can:

    1. Remove the \x-es
    2. Use .decode('hex') on the resulting string

    Code:

    >>> '\\xF3\\xBE\\x80\\x80'.replace('\\x', '').decode('hex')
    '\xf3\xbe\x80\x80'
    

    Note the appropriate interpretation of backslashes. When the string representation is '\xf3' it means it's a single-byte string with the byte value 0xF3. When it's '\\xf3', which is your input, it means a string consisting of 4 characters: \, x, f and 3