Bin to dec to hex in Python 3

You can find here how to convert between bin, dec, hex values in Python3.

Bin to Dec in Python 3

Function int(x, base=2)returns an integer object constructed from a number or string x with the base 2. [reference]

Example

How to convert value 11111111 in bin to dec in Python.

>>> int('11111111', 2)
Output: 255

Dec to Bin in Python 3

Function format(value[, format_spec]) converts a value to a “formatted” representation, as controlled by format_spec. [reference]

Example

How to convert value 5 in dec to bin in Python.

>>> format(5, "b")
Output: 101

Bin to Hex in Python 3

Function hex(x) converts an integer number to a lowercase hexadecimal string prefixed with “0x”. If x is not a Python int object, it has to define an __index__() method that returns an integer. [reference]

Example

How to convert value 101011 in bin to hex in Python.

>>> hex(int('101011', 2))
Output: '0x2b'

Hex to Bin in Python 3

Function bin(x) converts an integer number to a binary string prefixed with “0b”. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer. [reference]

Example

How to convert value 3ef in hex to bin in Python.

>>> bin(int("3ef", 16))
Output: '0b1111101111'

Dec to Hex in Python 3

Function hex(x) converts an integer number to a lowercase hexadecimal string prefixed with “0x”. If x is not a Python int object, it has to define an __index__() method that returns an integer. [reference]

Example

How to convert value 185 in dec to hex in Python.

>>> hex(185)
Output: '0xb9'

Hex to Dec in Python 3

Function int(x, base=16)returns an integer object constructed from a number or string x with the base 16. [reference]

Example

How to convert value 3ef in hex to dec in Python.

>>> int("3ef", 16)
Output: 1007