Editing
Python programming
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Converting binary string and text string == The [https://docs.python.org/3/library/binascii.html binascii] module contains a number of methods to convert between binary and various ASCII-encoded binary representations. <pre>>>> import binascii >>> b = binascii.hexlify(b'binary_string') < -- convert from binary string to binary hexadecimal representation...similar to bytes.hex() except this returns binary string >>> b b'62696e6172795f737472696e67' < -- binary hexadecimal representation >>> binascii.a2b_hex(b) <-- convert from binary hexadecimal to binary string b'binary_string' >>> bytes.hex(b) < -- convert binary hexadecimal b'62696e6172795f737472696e67' to octet strings (takes up twice as much space) '3632363936653631373237393566373337343732363936653637' >>> s = '1234567890' < -- create sample string >>> bo = bytes(s, 'ascii') < -- create bytes object of string (binary string) >>> bo b'1234567890' < -- notice the little b in front of the first single quote >>> octet_string = bytes.hex(bo) < -- convert bytes object (binary string) to octet string >>> octet_string '31323334353637383930'</pre> === Converting back and forth with binascii, bytes, ASCII symbols, and ASCII as hexadecimal === <pre>>>> import datetime >>> dt = datetime.datetime.utcnow() >>> dt datetime.datetime(2019, 4, 2, 1, 36, 8, 931977) >>> f = '{:%Y%m%d%H%M%S.%f}' >>> f.format(dt) '20190402013608.931977' >>> f.format(dt)[:-3]+'Z' '20190402013608.931Z' >>> t = f.format(dt)[:-3]+'Z' >>> t '20190402013608.931Z' >>> bytes(t,'ascii') b'20190402013608.931Z' >>> import binascii >>> binascii.hexlify(bytes(t,'ascii')) b'32303139303430323031333630382e3933315a' >>> binascii.hexlify(bytes(t,'ascii')) b'32303139303430323031333630382e3933315a' >>> bt = binascii.hexlify(bytes(t,'ascii')) >>> bt b'32303139303430323031333630382e3933315a' >>> bt.decode() '32303139303430323031333630382e3933315a' >>> '32303139303430323031333630382e3933315a'.encode() b'32303139303430323031333630382e3933315a' >>> b'32303139303430323031333630382e3933315a'.decode() '32303139303430323031333630382e3933315a' >>> binascii.unhexlify(bt) < -- change from binary ASCII encoded as hexadecimals to binary string of ASCII symbols b'20190402013608.931Z' >>> binascii.unhexlify(bt).decode() < -- do the same as above but also change from binary string to text string '20190402013608.931Z' >>> st = bt.decode() < -- convert binary string to text string >>> st '32303139303430323031333630382e3933315a' >>> bytearray.fromhex(st).decode() < -- change from text string ASCII encoded as hexadecimals to text string of ASCII symbols '20190402013608.931Z'</pre>
Summary:
Please note that all contributions to GotOpinion may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
GotOpinion:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
Edit source
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information