Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

What is binascii.hexlify in Python?

Sarvech Qadir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

widget

binascii is a widely used Python library for ASCII-encoded binary representations. It contains several methods for converting to binary from ASCII or hex, and vice versa.

binascii.hexlify is one of the methods in the binascii library. This method converts the binary representation of the data to hexadecimal. The conversion follows these principles:

  1. Every byte is converted to a 2-digit hexadecimal representation.

  2. The length of return data is twice the length of input data.

Syntax

binascii.hexlify(data[, sep[, bytes_per_sep=1]])

Parameters

  1. data: The data that needs to be converted.

  2. sep: We use this if a user wants to apply a separator between each converted character.

widget

Code

In this code, we see the working of binascii.hexlify(). First, we convert data to binary format using bytes method. Next, we use binascii.hexlify() to convert the binary data to hexadecimal and print the result. Additionally, we use a separator - to see how the second parameter works.

import binascii
x = "Hello"
res = bytes(x, 'utf-8')
print(binascii.hexlify(res))
print(binascii.hexlify(res , '-'))

RELATED TAGS

python

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring