binascii.b2a vs ord()

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Jan 10 05:09:14 EST 2021


On 10/01/21 4:29 pm, Bischoop wrote:

> So what's that binascii

The binascii module contains implementations of various ways of
encoding *binary* data as ascii text. The one you're using there
is a format called "uuencode"; it was frequently used in the
days before the WWW when people often sent arbitrary files to
each other by email. There's another one called "base64" which
is often used behind the scenes nowadays when you attach a file
to an email message.

If you're trying to convert between unicode strings (all text
strings are unicode in Python 3) and ascii byte strings, the
binascii module is NOT what you want.

Instead, you should be using the encode and decode methods of
the string objects themselves:

Python 3.8.2 (default, Mar 23 2020, 11:36:18)
[Clang 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> s = 'Hello World'
 >>> s
'Hello World'
 >>> b = s.encode('ascii')
 >>> b
b'Hello World'
 >>> s2 = b.decode('ascii')
 >>> s2
'Hello World'

-- 
Greg



More information about the Python-list mailing list