2

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

3 Answers 3

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

chr is the inverse of ord.

Sign up to request clarification or add additional context in comments.

3 Comments

chr, int and generator expressions, I think this is as built-in as you'll get :) If you meant something shorter, or a single function, the answer would be "no" as well. It would be over-specializing, which Python tends to avoid as much as possible.
What about if the oldString have hexa values, like f? int('f') fails.
If you expect to be converting hex (or some higher base encode-able alphanumerically) as well, then pass in a base to int(). You can go up to 36: int(x, 36).
2

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.

2 Comments

Thanks. But I may say that the string of my example is lengh 8 just by accident.
@Juanjo, sure, but were the length 10 instead of 8, the fraction of byte strings your method can be represent would diminish by other hundreds of times, etc. "Even for such really short strings", as I said -- it gets worse as string lengths become realistic!-)
2

For variety:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.