5

I'd like to dump a simple Unicode string into an array of bytes, so I can refer to each as an int. Is this possible?

I'm looking to take a string u"Hello World" and convert it into UTF-8 and something that looks like this: `

[0x01, 0x02, ..., 0x02]

How can I do this efficiently?

1
  • 3
    What do you mean refer to each as an int? Commented Jan 5, 2012 at 17:45

2 Answers 2

13

If you're looking for a Python bytearray:

my_array = bytearray(u"hello, world", encoding="utf-8")
Sign up to request clarification or add additional context in comments.

3 Comments

follow it with output = []; for i in ba: output.add(hex(i)) ;; print ", ".join(output). For the win!
There's an even easier way to do that: print ", ".join(map(hex, ba))
A one-liner to print the byte string without the 0x separators: return "".join(map(hex, bytearray(in_string, encoding="utf-8"))).replace('0x','')
8

Your question could mean two things: either encode the Unicode string using, say, UTF8 and getting a list of the resultant bytes, or getting a list of Unicode code points.

In the former case:

list_of_bytes = map(ord, my_unicode_string.encode('utf8'))

In the latter case:

list_of_code_points = map(ord, my_unicode_string)

4 Comments

Given that some (gosh, that's an understatement) unicode characters don't have code points in the range [0..255], the former seems more likely.
The former will give you an encoded version of the values, so for the character u'誠' you'd get [232, 170, 160], whereas the latter will give you the full Unicode point value (8AA0 -> 35488). Depending on what @TK Kocheran wants to accomplish, both of these methods work pretty well.
I'm looking for the first case, and thanks! Is there a way I can map hex() to each of the values so I have them in hexadecimal strings in the array?
Yeah: use lambda x: hex(ord(x)) instead of just ord.

Your Answer

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.