int given base 2 and then hex:

>>> int('010110', 2)
22
>>> hex(int('010110', 2))
'0x16'
>>> 

>>> hex(int('0000010010001101', 2))
'0x48d'

The doc of int:

int(x[, base]) -> integer

Convert a string or number to an integer, if possible.  A floating

point argument will be truncated towards zero (this does not include a string representation of a floating point number!) When converting a string, use the optional base. It is an error to supply a base when converting a non-string. If base is zero, the proper base is guessed based on the string content. If the argument is outside the integer range a long object will be returned instead.

The doc of hex:

hex(number) -> string

Return the hexadecimal representation of an integer or long

integer.

Answer from Eli Bendersky on Stack Overflow
Discussions

Python: binary/hex string conversion? - Stack Overflow
I have a string that has both binary and string characters and I would like to convert it to binary first, then to hex. ... How do I go about converting this string in Python so that the output in hex format is similar to this below? More on stackoverflow.com
🌐 stackoverflow.com
How can I convert a python binary string to hex? - Stack Overflow
binary_str = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' How can I convert this binary data to hex data in python? I tried like this but it's not working. >>> hex(b'\x00\x00\x00\x00\x00\x... More on stackoverflow.com
🌐 stackoverflow.com
How do I convert a string of bits to a hex string in Python? - Stack Overflow
I have a bit-string of 32 characters that I need to represent as hexadecimal in Python. For example, the string "10000011101000011010100010010111" needs to also be output as "83A1A897". Any sugges... More on stackoverflow.com
🌐 stackoverflow.com
Understanding how to convert binary to hexadecimal in python to control slave-chip
You could translate your calculations into an array of bits and then translate each group of 4 bits to hexadecimal digit. More on reddit.com
🌐 r/learnprogramming
5
1
June 5, 2023
People also ask

How can I convert binary to hex in Python?
There are multiple methods to convert binary to hex in Python. You can use built-in functions like `hex()` or manually convert each binary digit to its hexadecimal equivalent using an algorithm.

Both approaches will be explained in detail in the respective sections of this guide.
🌐
codingdeeply.com
codingdeeply.com › home › easily convert binary to hex in python: step-by-step guide
Easily Convert Binary to Hex in Python: Step-by-Step Guide
Why is converting binary to hex in Python useful?
Converting binary to hex in Python is useful in computer programming because hexadecimal numbers are often used to represent binary data in a more compact and readable format.

Hexadecimal numbers are also commonly used in various applications, such as network protocols and memory addresses.
🌐
codingdeeply.com
codingdeeply.com › home › easily convert binary to hex in python: step-by-step guide
Easily Convert Binary to Hex in Python: Step-by-Step Guide
Are there any built-in functions for converting binary to hex in Python?
Yes, Python provides a built-in function called `hex()` that can be used to directly convert a decimal or binary number to its hexadecimal representation.

This function simplifies the conversion process and is recommended for its convenience.
🌐
codingdeeply.com
codingdeeply.com › home › easily convert binary to hex in python: step-by-step guide
Easily Convert Binary to Hex in Python: Step-by-Step Guide
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python how-to's" › "how to convert binary to hex in python"
How to Convert Binary to Hex in Python | Delft Stack
February 2, 2024 - To convert binary to hexadecimal in Python, we can also use the int() function to first convert the binary string to an integer and then use the hex() function to obtain the hexadecimal representation.
🌐
Quora
quora.com › How-can-I-convert-binary-to-hex-in-Python
How to convert binary to hex in Python - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Convert Binary, Octal, Decimal, and Hexadecimal in Python | note.nkmk.me
May 19, 2023 - Built-in Functions - format() — Python 3.11.3 documentation · You can convert a number to a binary, octal, or hexadecimal string by specifying b, o, or x in the format specification string, which is used as the second argument of format().
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-convert-binary-to-hexadecimal
Python Program to Convert Binary to Hexadecimal - GeeksforGeeks
July 23, 2025 - ... # Python program to convert binary number # into hexadecimal number # Function calculates the decimal equivalent # to given binary number def binaryToDecimal(binary): binary1 = int(binary) decimal, i, n = 0, 0, 0 while(binary1 != 0): dec ...
Find elsewhere
🌐
Python
docs.python.org › 3 › library › binascii.html
binascii — Convert between binary and ASCII
The returned bytes object is therefore twice as long as the length of data. Similar functionality (but returning a text string) is also conveniently accessible using the bytes.hex() method.
🌐
Codingdeeply
codingdeeply.com › home › easily convert binary to hex in python: step-by-step guide
Easily Convert Binary to Hex in Python: Step-by-Step Guide
February 23, 2024 - We then use the int() function to convert the binary string to an integer, using 2 as the base. Finally, we pass the resulting integer to the hex() function to obtain the hexadecimal equivalent stored in the hex_num variable.
🌐
Joernhees
joernhees.de › blog › 2010 › 09 › 21 › how-to-convert-hex-strings-to-binary-ascii-strings-in-python-incl-8bit-space
How to convert hex strings to binary ascii strings in python (incl. 8bit space) | Jörn's Blog
The solution is based on the Python 2.6 new string formatting: ... Which can be decomposed into 4 bit for each hex char like this: (notice the 04b, which means 0-padded 4chars long binary string): >>> "{0:04b}".format(int("c",16)) + "{0:04b}".format(int("3",16)) '11000011' OK, now we could easily do this for all hex chars "".join(["{0:04b}".format(int(c,16)) for c in "c3a4c3b6"]) and done, but usually we want a blank every 8 bits from the right to left…
🌐
Educative
educative.io › answers › how-to-convert-a-binary-number-to-hexadecimal-in-python
How to convert a binary number to hexadecimal in Python
Below is the coding implementation for the mentioned method, where we are converting a binary string to a decimal number and storing it as a variable named decimal_number: ... We're given a binary number. ... The hex method is the built-in function used to convert a decimal number to a hexadecimal number.
🌐
Stack Overflow
stackoverflow.com › questions › 66586316 › how-can-i-convert-a-python-binary-string-to-hex
How can I convert a python binary string to hex? - Stack Overflow
We on StackOverflow expect you ... ... In python, hex does not work like that. Try it like this: binary_str = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' print(binary_str.hex())...
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-bytearray-to-hexadecimal-string-using-python
How to Convert Bytearray to Hexadecimal String using Python?
January 3, 2024 - import binascii byte_array = bytearray(b'Hello, World!') hex_string = binascii.hexlify(byte_array).decode('utf-8') print("Hexadecimal string:", hex_string) ... byte_array = bytearray(b'Python') hex_string = ''.join([format(byte, '02x') for byte in byte_array]) print("Hexadecimal string:", hex_string)
🌐
alpharithms
alpharithms.com › home › tutorials › how to convert hexadecimal values to binary in python
How to Convert Hexadecimal Values to Binary in Python - αlphαrithms
June 16, 2022 - A leading sign prefix ('+'/'-') is handled by inserting the padding after the sign character rather than before. The original string is returned if width is less than or equal to len(s). The approach to convert a hexadecimal value into a binary value in Python is simple enough.
🌐
Reddit
reddit.com › r/learnprogramming › understanding how to convert binary to hexadecimal in python to control slave-chip
r/learnprogramming on Reddit: Understanding how to convert binary to hexadecimal in python to control slave-chip
June 5, 2023 -

Hi,

I'm not entirely sure this is the correct place to ask. I've a chip I am trying to control using the NI-8452. This is all based on an evaluation board and due to urgency I can't wait much for the supplier to respond. The evaluation board needs to receive a 48-bit message that can be then send in hexadecimal in python under the format 00NY::XX-XX-XX-XX-XX-XX where NY are the registers and XX would be hexadecimals. I only need to access the 23 last bits. I need to modify chunks of 6 bits and I calculate these 6 bits individually.

So from MSB:LSB I only need to access 23:0 in chunks of 5:0, those are power levels that I can calculate easily by doing round(ThereticalPower/MyDiscreteLevel). What I am struggling (and I'm pretty sure it is easy) is how to generate the hexadecimals for 48 bits (so the 12 hexa values) when changing this, so for instance I know if I have 11:6 == 63 and 5:0 == 63 I need to have 00-00-00-00-0f-ff. But I have no idea how to get there.

EDIT: I managed to do it by filling the binary

binary = bin(decimal)[2:]  # Convert decimal to binary string
binary = binary.zfill(num_bits)

Then I add the multiple 6-bit binaries and convert using the function described here

🌐
Reddit
reddit.com › r/python › code to convert from binary to hexadecimal
r/Python on Reddit: Code to convert from binary to hexadecimal
February 28, 2022 - this code is kinda messy but ignore that def Hexdecimal_conv(a): if a == ["0", "0", "0", "0"]: return "0" if a == ["0", "0", "0", "1"]: return '1' if…
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-convert-string-to-binary
Convert String to Binary - Python - GeeksforGeeks
July 12, 2025 - Python · import binascii txt = ... turns those bytes into a hexadecimal string · bin(int(..., 16))[2:] converts the hex string to binary, removing the '0b' prefix ·...
🌐
Javatpoint
javatpoint.com › how-to-convert-hexadecimal-to-binary-in-python
How to convert hexadecimal to binary in python - Javatpoint
How to convert hexadecimal to binary in python with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.