## Base Conversion in Python

Python provides function to convert one data type to another Which is known as Type Casting.

**Base Conversion in python** : You can also convert one base to another base in python.

**What is base conversion?**

Converting one base value to another base value is called Base Conversion.

Electronic and Digital systems may use a variety of different number systems, (e.g. Decimal, Hexadecimal, Octal, Binary).

Converting one base to another base requires lot of efforts and having different logic also.

But, Python provides inbuilt function for base conversion.

**Inbuilt function for base conversion in python**

1. Binary Converter

2. hex converter

3. octal converter

**1. Binary Converter**

Python provides bin() function to convert any base value to binary value.

Syntax for bin function: bin(Value)

The return type of bin() is int type.

Example for bin() :- x=10

bin(x) => 1010

**2. Octal Converter**

Python provides oct() function to convert any base value to octal value.

Syntax for oct() function: oct(Value)

The return type of oct() is int type.

Example for oct() :- x=786

oct(x)

**3. Hex Converter**

Python provides Hex() function to convert any base value to Hex value.

Syntax for Hex() function: Hex(Value)

The return type of Hex() is int type.

Example for Hex() :- x=0101

Hex(x) => 5

**Hex to Decimal converter**

Converting Hex to decimal

**Example:- x= 0XFace Hex value**

Whenever we assign Hex value to variable, internally it stores decimal value.

That is whenever we use Hex, internally python pretends it as decimal value. You don’t require converting HEX to Decimal explicitly.

That is why we don’t have function to convert Hex to Decimal.

**Note** :- In Base Conversion provided value and return type value are of type int.

## Leave a Reply