Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

3 of 3
added 462 characters in body
Juicy
  • 12.6k
  • 40
  • 135
  • 221

Converting UTF-16 to UTF-8

I've loading a string from a file. When I print out the string with:

print my_string
print binascii.hexlify(my_string)

I get:

2DF5
0032004400460035

Meaning this string is UTF-16. I would like to convert this string to UTF-8 so that the above code produces this output:

2DF5
32444635

I've tried:

my_string.decode('utf-8')

Which output:

32004400460035

EDIT:

Here's a quick sample:

 hello = 'hello'.encode('utf-16')
 print hello
 print binascii.hexlify(hello)
 hello = hello[2:].decode('utf-8')
 print hello
 print binascii.hexlify(hello)

Which produces this output:

��hello
fffe680065006c006c006f00
hello
680065006c006c006f00

Expected output would be:

��hello
fffe680065006c006c006f00
hello
68656c6c6f
Juicy
  • 12.6k
  • 40
  • 135
  • 221
lang-py

AltStyle によって変換されたページ (->オリジナル) /