rakshithnrr
rakshithnrr

Reputation: 53

How to Convert from ISO-8859-1 format to UTF-8 and then into hex format string python

How do I convert the UTF-8 format character '戧' to a hex value and store it as a string "0xe6 0x88 0xa7".

with open(fromFilename,  encoding = "ISO-8859-1") as f:
    while True:
        c = f.read(1)
        if not c:
            print ("End of file")
            break
        print ("Read a character: %c", c)
        newC = repr(c.encode('utf-8'))
        print ("Read a decode character: %c", newC)
        newString = newString + newC

This is my code. Please let me know what's wrong.

Upvotes: 0

Views: 689

Answers (1)

Devstr
Devstr

Reputation: 4641

This works for me in Python 3.7

a = '戧'
encoded_bytes = a.encode(encoding='utf-8')
print(' '.join([hex(b) for b in encoded_bytes]))

>>> 0xe6 0x88 0xa7

Upvotes: 1

Related Questions