[PYTHON] Hexadecimal string and string conversion

Python

String => Hexadecimal string

The binascii module can convert byte strings to hexadecimal strings. If it is a Unicode character string, encode it before converting.

Python2/Python3


import binascii
binascii.hexlify(b'Hello') # => b'48656c6c6f'
binascii.b2a_hex(b'Hello') # => b'48656c6c6f'
binascii.hexlify(u'Hello'.encode('utf-8')) # => b'e38193e38293e381abe381a1e381af'
binascii.b2a_hex(u'Hello'.encode('utf-8')) # => b'e38193e38293e381abe381a1e381af'

You can also use the codecs module.

Python2/Python3


import codecs
codecs.encode(b'Hello', 'hex_codec') # => b'48656c6c6f'
codecs.encode(u'Hello'.encode('utf-8'), 'hex_codec') # => b'e38193e38293e381abe381a1e381af'

In Python2, you can also convert with str.encode.

Python2 only


'Hello'.encode('hex') # => '48656c6c6f'

Hexadecimal string => string

The binascii module can convert hexadecimal strings to byte strings. If you want to unicode, decode the result.

Python2/Python3


import binascii
binascii.unhexlify(b'48656c6c6f') # => b'Hello'
binascii.a2b_hex(b'48656c6c6f')   # => b'Hello'
binascii.unhexlify(b'e38193e38293e381abe381a1e381af').decode('utf-8') # => 'Hello'
binascii.a2b_hex(b'e38193e38293e381abe381a1e381af').decode('utf-8') # => 'Hello'

You can also use the codecs module.

Python2/Python3


import codecs
codecs.decode(b'48656c6c6f', 'hex_codec') # => b'Hello'
codecs.decode(b'e38193e38293e381abe381a1e381af', 'hex_codec').decode('utf-8') # => 'Hello'

In Python2, you can also convert with str.decode.

Python2 only


'48656c6c6f'.decode('hex') # => 'Hello'

Ruby

String => Hexadecimal string

You can use String # unpack to convert a string to a hexadecimal string.

'Hello'.unpack('H*') # => ["48656c6c6f"]
'Hello'.unpack('H*') # => ["e38193e38293e381abe381a1e381af"]

Hexadecimal string => string

You can use Array # pack to convert a hexadecimal string to a string. Change the character code information with String # force_encoding if necessary.

['48656c6c6f'].pack('H*') # => "Hello"
['e38193e38293e381abe381a1e381af'].pack('H*').force_encoding('utf-8') # => "Hello"

Bash

String => Hexadecimal string

You can convert a string to a hexadecimal string with xxd.

echo -n Hello | xxd -p # => 48656c6c6f
echo -n Hello| xxd -p # => e38193e38293e381abe381a1e381af

Hexadecimal string => string

You can convert a hexadecimal string to a string with xxd.

echo -n 48656c6c6f | xxd -r -p # => Hello
echo -n e38193e38293e381abe381a1e381af | xxd -r -p # =>Hello

MySQL

MySQL :: MySQL 5.6 Reference Manual :: 9.1.4 Hexadecimal Literal

String => Hexadecimal string

SELECT HEX('Hello');
-- 48656C6C6F

SELECT HEX("Hello")
-- E38193E38293E381ABE381A1E381AF

Hexadecimal string => string

SELECT x'48656C6C6F';
-- Hello

SELECT x'E38193E38293E381ABE381A1E381AF'
--Hello

Recommended Posts

Hexadecimal string and string conversion
Convert hexadecimal string to binary
Conversion between unixtime and datetime
Python indentation and string format
Date and time ⇔ character string
Efficient conversion method to byte string
python string processing map and lambda
Conversion of string <-> date (date, datetime) in Python
Python UTC ⇔ JST, character string (UTC) ⇒ JST conversion memo
String conversion of a list containing numbers
Conversion between singular and plural of words
[Introduction to AWS] Text-Voice conversion and playing ♪
String → Bool value conversion in Python Consideration