Skip to content Skip to sidebar Skip to footer

In Python 3, How Can I Convert Ascii To String, *without Encoding/decoding*

Python 3.6 I converted a string from utf8 to this: b'\xe6\x88\x91\xe6\xb2\xa1\xe6\x9c\x89\xe7\x94\xb5@xn--ssdcsrs-2e1xt16k.com.au' I now want that chunk of ascii back into string f

Solution 1:

The (wrong) answer is quite simple:

chr(asciiCode)

In your special case:

myString = ""forcharin b'\xe6\x88\x91\xe6\xb2\xa1\xe6\x9c\x89\xe7\x94\xb5@xn--ssdcsrs-2e1xt16k.com.au':
    myString+=chr(char)
print(myString)

gives:

ææ²¡æçµ@xn--ssdcsrs-2e1xt16k.com.au

Maybe you are also interested in the right answer? It will probably not please you, because it says you have ALWAYS to deal with encoding/decoding ... because myString is now both UTF-8 and ASCII at the same time (exactly as it already was before you have "converted" it to ASCII).

Notice that how myString shows up when you print it will depend on the implicit encoding/decoding used by print.

In other words ...

there is NO WAY to avoid encoding/decoding

but there is a way of doing it a not explicit way.

I suppose that reading my answer provided HERE: Converting UTF-8 (in literal) to Umlaute will help you much in understanding the whole encoding/decoding thing.

Solution 2:

What you have there is not ASCII, as it contains for instance the byte \xe6, which is higher than 127. It's still UTF8.

The representation of the string (with the 'b' at the start, then a ', then a '\', ...), that is ASCII. You get it with repr(yourstring). But the contents of the string that you're printing is UTF8.

But I don't think you need to turn that back into an UTF8 string, but it may depend on the rest of your code.

Post a Comment for "In Python 3, How Can I Convert Ascii To String, *without Encoding/decoding*"