Python

Convert binary, octal, decimal, and hexadecimal in Python

In Python, you can handle numbers and strings as binary (bin), octal (oct), and hexadecimal (hex) as well as decimal. They can also be converted to each other.
This article describes the following contents.
Write integers in binary, octal, and hexadecimal notation
Convert a numbers to a binary, octal, and hexadecimal string
bin(), oct(), hex()
format(), str.format(), f-strings
Convert a negative integer to a string in two's complement representation
Convert a binary, octal, and hexadecimal string to a number
int()
Usage examples
Binary string arithmetic
Convert between binary, octal, and hexadecimal numbers
See the following article for the basics of conversion between the string (str) and the number (int, float).

Write integers in binary, octal, and hexadecimal notation

By adding the prefixes 0b, 0o, and 0x, integer type int numbers can be written in binary, octal, and hexadecimal, respectively. The output of print() will be in decimal notation.
bin_num = 0b10
oct_num = 0o10
hex_num = 0x10

print(bin_num)
print(oct_num)
print(hex_num)
# 2
# 8
# 16

source:
You can also use uppercase 0B, 0O, and 0X.
Bin_num = 0B10
Oct_num = 0O10
Hex_num = 0X10

print(Bin_num)
print(Oct_num)
print(Hex_num)
# 2
# 8
# 16

source:
Even with a prefix, the type is an integer int.
print(type(bin_num))
print(type(oct_num))
print(type(hex_num))
# <class 'int'>
# <class 'int'>
# <class 'int'>

print(type(Bin_num))
print(type(Oct_num))
print(type(Hex_num))
# <class 'int'>
# <class 'int'>
# <class 'int'>

source:
Since they are int, they can be operated as usual.
result = 0b10 * 0o10 + 0x10
print(result)
# 32

source:
In Python3.6 and later, you can insert underscores _ in numbers.Repeating the underscore _ raises an error, but you can insert as many as you like if it is not repeated.
The underscore _ can be used as a delimiter when there are many digits. For example, it is easier to read if you enter _ every four digits.
print(0b111111111111 == 0b1_1_1_1_1_1_1_1_1_1_1_1)
# True

bin_num = 0b1111_1111_1111
print(bin_num)
# 4095

source:

Convert a number to a binary, octal, and hexadecimal string

You can use the following functions to convert a number to a binary, octal, or hexadecimal string.
Built-in function bin(), oct(), hex()
Built-in function format(), string method str.format(), f-strings
It also describes how to get a string in two's complement representation for a negative value.

bin(), oct(), hex()

The built-in functions bin(), oct(), and hex() can be used to convert a number to a binary, octal, and hexadecimal string. These functions return a string with the prefixes 0b,0o, and 0x, respectively.
i = 255

print(bin(i))
print(oct(i))
print(hex(i))
# 0b11111111
# 0o377
# 0xff

print(type(bin(i)))
print(type(oct(i)))
print(type(hex(i)))
# <class 'str'>
# <class 'str'>
# <class 'str'>

source:
If you don't need the prefix, use slice [2:] to extract the trailing strings or use format() as described next.
print(bin(i)[2:])
print(oct(i)[2:])
print(hex(i)[2:])
# 11111111
# 377
# ff

source:
If you want to convert to a decimal string, use str().
print(str(i))
# 255

print(type(str(i)))
# <class 'str'>

source:

format(), str.format(), f-strings

The built-in function format(), the string method str.format(), and f-strings can also be used to convert a number to a binary, octal, and hexadecimal string.
By specifying b, o and x in the format specification string of the second argument of format(), a number can be converted to a binary, octal, and hexadecimal string, respectively.
print(format(i, 'b'))
print(format(i, 'o'))
print(format(i, 'x'))
# 11111111
# 377
# ff

print(type(format(i, 'b')))
print(type(format(i, 'o')))
print(type(format(i, 'x')))
# <class 'str'>
# <class 'str'>
# <class 'str'>

source:
If you want to get the character string with the prefix 0b, 0o, 0x, add # to the format specification string.
print(format(i, '#b'))
print(format(i, '#o'))
print(format(i, '#x'))
# 0b11111111
# 0o377
# 0xff

source:
It is also possible to fill in zero (0) with any number of digits. Note that the number of characters for the prefix (two characters) must also be taken into account when filling in zero with a prefix.
print(format(i, '08b'))
print(format(i, '08o'))
print(format(i, '08x'))
# 11111111
# 00000377
# 000000ff

print(format(i, '#010b'))
print(format(i, '#010o'))
print(format(i, '#010x'))
# 0b11111111
# 0o00000377
# 0x000000ff

source:
The string method str.format() can be used for the same conversion.
print('{:08b}'.format(i))
print('{:08o}'.format(i))
print('{:08x}'.format(i))
# 11111111
# 00000377
# 000000ff

source:
In Python3.6 or later, you can also use the f-strings (f'xxx').
print(f'{i:08b}')
print(f'{i:08o}')
print(f'{i:08x}')
# 11111111
# 00000377
# 000000ff

source:

Convert a negative integer to a string in two's complement representation

When using bin() or format(), negative integers are converted to absolute values with a minus sign.
x = -9

print(x)
print(bin(x))
# -9
# -0b1001

source:
In Python, bitwise operations on negative integers are performed in two's complement representation, so if you want to get a string expressed in two's complement representation, take the bitwise-and & of the maximum number of digits required, for example, 0b1111 (= 0xf) for 4bit, 0xff for 8bit, and 0xffff for 16bit.
print(bin(x & 0xff))
print(format(x & 0xffff, 'x'))
# 0b11110111
# fff7
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.