Python’s binascii – hexlify() and unhexlify()
What the heck?

Today, a dear friend of mine came up to me and asked about the Python module binascii – particularly about the methods hexlify() and unhexlify(). Since he asked for it, I’m going to share my answer publicly with you.

First of all, I’m defining the used nomenclature:

  • ASCII characters are being written in single quotes
  • decimal numbers are of the type Long with a L suffix
  • hex values have a x prefix

First, let me quote the documentation:

Return the hexadecimal representation of the binary data. Every byte of data is converted into the corresponding 2-digit hex representation. The resulting string is therefore twice as long as the length of data.
Return the binary data represented by the hexadecimal string hexstr. This function is the inverse of b2a_hex()hexstr must contain an even number of hexadecimal digits (which can be upper or lower case), otherwise a TypeError is raised.

I’ll begin with hexlify(). As the documentation states, this method splits a string which consists of hex-tuples into distinct bytes.

The ASCII character ‘A’ has 65L as numerical representation. To verify this in Python:

>>> long(ord('A'))

You might ask “Why is this even relevant to understand binascii?” Well, we don’t know anything about how ord() does its job. But with binascii we can re-calculate manually and verify.

>>> binascii.hexlify('A')

Now we know that an ‘A’ – interpreted as binary data and shown in hex – resembles ’41’. But wait, ’41’ is a string and no hex value! That’s no biggy, hexlify() represents its result as string.

To stay with the example, let’s convert 41 into a decimal number and check if it equals 65L.

>>> long('41', 16)

Tada! It seems that ‘A’ = 41 = 65L.
You might have known that already, but please, stay with me a minute longer.

To make it look a little more complex:

>>> binascii.hexlify('A') == "%X" % long('41', 16)

Be aware that

>>> "%X" %n

converts a decimal number into its hex representation.


binascii.unhexlify() naturally does the same thing as hexlify(), but in reverse. It takes binary data and displays it in tuples of hex-values.

I’ll start off with an example:

	>>> binascii.unhexlify('41')

	>>> binascii.unhexlify("%X" % ord('A'))

Here, unhexlify() takes the numerical representation 65L from the ASCII character ‘A’

	>>> ord('A')

converts it into hex 41

	>>> "%X" % ord('A')

and represents it as a 1-tuple (meaning dimension of one) of hex values.

And now the conclusio – why might all of this be useful?
Right now, I can think of at least four use cases:

  • cryptography
  • data-transformation (i.e. Base64 for MIME/E-Mail attachements)
  • security (deciphering binary readings off a network, pattern matching, …)
  • textual representation of escape sequences

Taking up the last example, I’ll show you how to visualize the Bell esape sequence (you know, that thing that keeps beeping in your terminal).
Taken from the ASCII table, the numerical representation of the Bell is 7. Programmers might know it better as a.

	>>> '7' == 'a'

Presuming you read such a character in some kind of binary data – for example from a socket

	>>> foo = '7'

and you want to visualize this data

	>>> print foo

you will not get any results – at least none visible. You might hear the Bell sound if you’re not on a silent terminal.

Now, finally – binascii to the rescue:

	>>> binascii.hexlify('7')

Voilà, the dubious string is decrypted.

Category: articles | Tags: , , , , , , , , 5 comments »

5 Responses to “Python’s binascii – hexlify() and unhexlify()
What the heck?

  1. Aaron Curtis

    Surely you meant to title this:

    Python’s binascii – hexlify() and unhexlify()
    What the hex?

  2. Jacob Maines


    First of all: you used “≶≶≶ ” in a PLAIN-TEXT field.
    You should have used “>>> ” instead!

    That is all…Or is it??

  3. Kevin

    How is 65L an integer? What is the deal with the L?

  4. DonaldZes

    nice website

  5. Andrewobest

    Имеется такая услуга – добровольное медицинское обслуживание (или ДМО).
    Она предполагает, что вы вносите небольшую сумму за то, что ходит на прием весь год бесплатно.
    Однако опросы показали, что лишь 5% жителей Санкт-Петербурга знают об этом.
    По какой причине?
    Да потому что клиникам выгоднее брать плату за каждый визит.
    А если какой-нибудь сотрудник клиники попытается посоветовать добровольное медицинское обслуживание клиенту – это сулит ему увольнением.
    Информация о ДМО уже спровоцировала кучу скандалов, после того как информацию об этом распространил один возмущенный врач.
    Его уволили , после того, как он посоветовал ДМО постоянному клиенту.
    Самое невероятное, что информация по ДМО находятся в открытом доступе, просто натыкались на эту информацию единицы.
    Как отстоять свои права?
    О правилах оказания такой услуги и обязанностях частных клиник можно узнать, сделав запрос в Яндексе: “добровольное медицинское обслуживание”.
    И именно обслуживание, а не страхование.