Python Binary, Octal & Hexadecimal numbers and Base Conversion

Aditya Garg
4 min readOct 18, 2023

--

Overview of Number Base/Radix

In a positional numeral system, Radix (plural: radices) or base is the number of unique digits, including the digit 0, used to represent numbers.

Base conversion in Python

There are already existing articles on Medium and GeeksforGeeks regarding Non-Base-10 numbers:

This article discusses only built-in methods to convert between bases in Python.

Corresponding GitHub file: https://github.com/AdityaGarg1995/BeginnerPython/blob/main/Binary%2C%20Octal%20and%20Hexadecimal%20Numbers.ipynb

Converting from Any Built-in base to Any other Built-in base

format() function allows converting from any given base to another. It takes two arguments: a number of a given base and the base representing character of the output base.

This is applicable for Decimal, Binary, Octal and Hexadecimal bases as these are built-in in Python.

Bases with corresponding base representing characters are as follows:

  • Decimal : ‘d’
  • Binary: ‘b’
  • Octal: ‘o’
  • Hexadecimal: ‘x’

Examples given below:


print(format(255, 'b')) # base 10 to base 2
print(format(0b100100101, 'd')) # Base 2 to base 10
print(format(0x1fa, 'o')) # base 16 to base 8
print(format(255, 'x')) # Base 10 base 16


print("Octal to binary", bin(0o253))
print(type(bin(0o253)))
print("Hexadecimal to Decimal", int(0x253))
print(type(int(0x253)))
print("Binary to Hexadecimal", hex(0b100100))
print(type(hex(0b100100)))
print("Binary to Octal", oct(0b100100))
print(type(oct(0b100100)))

Converting from Base-10 to Non-Base-10

Python provides built-in functions to convert from Base 10 integer to Binary, Octal and Hexadecimal representations. The respective functions are bin(), oct() and hex() — the returned values are of String type prefixed with the corresponding base: 0b for bin(), 0o for oct() and 0x for hex(). In case you don’t need these prefixes, simply use replace() method as with usual strings.

Please note that only numbers with bases 2, 8, 10 and 16 (i.e. binary, octal, decimal and hexadecimal numbers) can be given as input to bin(), oct() and hex().

## Example with input integer = 9

print("Integer to Binary", bin(9))
print(type(bin(9)))
print("Integer to Octal", oct(9))
print(type(oct(9)))
print("Integer to Hexadecimal", hex(9))
print(type(hex(9)))


## Example with input integer = -9

print("Integer to Binary", bin(-9))
print(type(bin(9)))
print("Integer to Octal", oct(-9))
print(type(oct(9)))
print("Integer to Hexadecimal", hex(-9))
print(type(hex(9)))

Converting from Non-Base-10 to Base-10

Inputting Binary, Octal or Hexadecimal values into print() function results in their corresponding integer value being displayed as output.

Inputting Binary, Octal or Hexadecimal values into int() function results in their corresponding integer value being returned as output, which can then be printed using print() function or stored in a variable.

## Converting from Non-Base-10 to Base-10 using print() function

print("Binary to Integer using print() function: ", end='')
print(0b1001)
print("Octal to Integer using print() function: ", end='')
print(0o11)
print("Hexadecimal to Integer using print() function: ", end='')
print(0x9)

## Converting from Non-Base-10 to Base-10 using int() function
print("Binary to Integer using int() function: ", end='')
print((int(0b100011001101)))
print("Octal to Integer using int() function: ", end='')
print(int(0o21165))
print("Hexadecimal to Integer using int() function: ", end='')
print(int(0x26479))

--

--

Responses (1)