1

I'm trying to figure out how to use the unicode support in python; I would like to convert this string to unicode : "ABCDE" --> "\x00A\x00B\x00C\x00D\x00E"

Any built-in functionnality can do that, or shall i use join() ?

Thanks !

asked Dec 2, 2010 at 0:31

3 Answers 3

5

That's UTF-16BE, not Unicode.

>>> 'ABCDE'.decode('ascii').encode('utf-16be')
'\x00A\x00B\x00C\x00D\x00E'
answered Dec 2, 2010 at 0:34
Sign up to request clarification or add additional context in comments.

2 Comments

why would you put in the .decode('ascii') bit? It's implied by the .encode('utf-16be') ('\xff'.encode('utf-16be') will fail with an UnicodeDecodeError ascii codec error the same way as '\xff'.decode('ascii') will)
@Chris: To make it explicit to the reader that it has to be decoded somehow.
2

The key to understanding unicode in python is that unicode means UNICODE. A unicode object is an idealized representation to the characters, not actual bytes.

answered Dec 2, 2010 at 0:36

Comments

0

the str object should be firstly converted to unicode object by decode method. then convert the unicode object to str object using encode method with character-encoding you want.

answered Mar 9, 2012 at 8:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.