Javatpoint Logo
Javatpoint Logo

How to convert hexadecimal to binary in python?

The "hex" is an abbreviation for Hexadecimal. It is a numbering system that uses 16 as its base. It is commonly used in computing and digital electronics because it can represent a byte (8 bits) of data with just two digits, which makes it more concise and easier to read than binary. In hexadecimal, the digits 0-9 represent their respective values, while the letters A-F (or a-f) represent values 10-15, respectively.

Method 1: Using bin() function

Converting hexadecimal to binary in Python can be done using a built-in function called bin(). This function takes an integer or a string as input and returns its binary equivalent.

Example:

Output:

11010

Method 2: Using Bitwise Operators

Python provides built-in bitwise operators that can be used to manipulate binary data. You can use these operators to convert a hexadecimal string to binary by first converting the string to an integer, and then using bitwise operations to extract the binary digits.

Example:

Here is an example code snippet:

Output:

11111

Explanation:

In this code, we first convert the hexadecimal string to an integer using the int() function with the base set to 16. After that, we use the format() function with the format specifier 'b' to convert the integer to a binary string. The format() function returns a string with leading zeros as needed to represent the binary value. Finally, we print the binary string.

Method 3: Using the hex2bin() Function from binascii Module

Python's binascii module provides a hex2bin() function that can be used to convert a hexadecimal string to binary.

Example:

Here is an example code snippet:

Output:

0b11010

Explanation:

In this code, we first import the binascii module. After that, we define the hexadecimal string we want to convert. We pass the hexadecimal string to binascii.unhexlify() function to convert it to a bytes object. After that, we convert the byte object to an integer using the int.from_bytes() function, with the byte order set to 'big' (most significant byte first). Finally, we pass the integer to the bin() function to get the binary string representation.

Note: The bin() function returns the binary string with a '0b' prefix. If you want to remove the prefix, you can use string slicing.

Method 4: Using a List Comprehension and String Formatting

We can convert a hexadecimal string to binary using a list comprehension that iterates over each hexadecimal digit in the string and converts it to its binary equivalent using string formatting.

Example:

Output:

00011010

Explanation:

In this code, we first define the hexadecimal string we want to convert. After that, we use a list comprehension to iterate over each hexadecimal digit in the string. For each digit, we convert it to an integer using the int() function with the base set to 16, and then format it as a binary string with 4 digits using the '{0:04b}'.format() string formatting syntax. The resulting binary strings are collected into a list, which is then joined together into a single string using the ''.join() function.

Method 5: Using the NumPy Library

If you have the NumPy library installed, you can use the numpy.base_repr() function to convert a hexadecimal string to binary.

Example:

Output:

11010

Note: The numpy.base_repr() function returns the binary string without any prefix or leading zeros, unlike the built-in bin() function.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA