1. 用encode与decode来实现base64编码:
#!/usr/bin/python
str = "this is string example....wow!!!";
str = str.encode('base64','strict');
print "Encoded String: " + str;
print "Decoded String: " + str.decode('base64','strict')
运行结果如下:
beyond@beyond ~/code/code-python $ python Base64test.py
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
Decoded String: this is string example....wow!!!
2. python将字符串转化成01串,还可以扩展,将下面换成0:02x就是16进制串:
''.join([ '{0:08b}'.format(ord(i)) for i in 'hello world' ])
>>> ''.join([ '{0:08b}'.format(ord(i)) for i in 'hello world' ])
'011010000