Juanjo Conti
Juanjo Conti

Reputation: 30013

How to convert a Python string representing bytes into actual bytes?

I have a string like: "01030009" and I want to get another string (because in Python 2.x we use strings for bytes) newString which will produce this result:

for a in newString:
    print ord(a)

0
1
0
3
0
0
0
9

Thanks

Upvotes: 2

Views: 2121

Answers (3)

Marius Gedminas
Marius Gedminas

Reputation: 11337

For variety:

import string
newString = oldString.translate(string.maketrans('0123456789',
                '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09'))

Upvotes: 2

Alex Martelli
Alex Martelli

Reputation: 881517

All the "deeply builtin" ways interpret characters as bytes in a different way than the one you want, because the way you appear to desire seems limited to represent bytes worth less than 10 (or less than 16 if you meant to use hex and just completely forgot to mention it). In other words, your desired code can represent a truly miniscule fraction of byte strings, and therefore would be absurd to "officialize" in any way (such as supporting it in builtin ways)!

For example, considering strings of length 8 (your example's short length), the total number of byte strings of that length which exist is 256 ** 8, while the number your chosen notation can represent is 10 ** 8. I.e....:

>>> exist = 256 ** 8
>>> repre = 10 ** 8
>>> print exist, repre
18446744073709551616 100000000
>>> print (repre / float(exist))
5.42101086243e-12
>>> 

So why would you expect any kind of "built-in" official support for a representation which, even for such really short strings, can only represent about five thousandths of one billionth of the possible byte strings?! The words "special case" were invented for things that happen far more frequently than this (if you got a random 8-byte string every second, it would be many centuries before you finally got one representable in your scheme), and longer byte strings keep exacerbating this effect exponentially, of course.

There are many "official" schemes for representation of byte strings, such as base64 and friends as specified in RFC 3548... your desired scheme is very signally not among them;-). Those are the schemes that get "official", built-in support in Python, of course.

Upvotes: 2

Amber
Amber

Reputation: 526533

''.join(chr(int(x)) for x in oldString)

chr is the inverse of ord.

Upvotes: 8

Related Questions