Conversion between binary, hexadecimal and decimal numbers using Coden module


The coden is a python library developed by Tanmay Earappa, which is used for secret codes (decoding and encoding secret codes). This module provides functionalities to do the code conversions. Following are few functions and their functionalities provided by this module –

  • coden.secret(): It is used for decode or encode the secret codes based on the input of the mode parameter.

  • hex_to_bin(): It will do the Hexadecimal to Binary conversion.

  • int_to_bin(): It will do the Decimal to Binary conversion.

  • int_to_hex(): It will do the Decimal to Hexadecimal conversion

Install Coden with pip

With the pip command we can easily install this module, then by importing the module we can access the conversion functionalities to the python interpreter. Just by running the following commands in the command prompt we will get the Coden module.

pip install --coden

In this article, we will discuss Conversion between binary, hexadecimal and decimal numbers using Coden module

Hexadecimal to Binary conversion

Using the hex_to_bin() function we can do the conversion of a hexadecimal number to Binary number is possible.

Example

The input hexadecimal number is f1ff and the expected binary number will be 1111000111111111.

# importing the coden module
import coden

hexadecimal_number="f1ff" 
print("Input Hexadecimal Number:", hexadecimal_number)

binary_output = coden.hex_to_bin(hexadecimal_number)
print('Binary Output:', binary_output)

Output

Input Hexadecimal Number: f1ff
Binary Output: 1111000111111111

Hexadecimal to Decimal Conversion

The function coden.hex _to_int () converts a Hexadecimal number into a decimal number.

Example

Let’s take a hexadecimal number and convert it to the decimal.

import coden

hexadecimal_number="f1ff63" 
print("Input Hexadecimal Number:", hexadecimal_number)

decimal_number = coden.hex_to_int(hexadecimal_number)
print('Decimal Output:', decimal_number)

Output

Input Hexadecimal Number: f1ff63
Decimal Output: 15859555

Binary to Hexadecimal Conversion

The function coden.bin_to_hex() converts the Binary number to Hexadecimal number.

Example

Here we will take a binary number and expected hexadecimal number will be abc123efff.

import coden

binary_number = '001010101111000001001000111110111111111111'
print("Input Binary Number:", binary_number)

# Convert Binary Number to Hexadecimal Number
hexadecimal_output = coden.bin_to_hex(binary_number)
print('Hexadecimal Output:', hexadecimal_output)

Output

Input Binary Number: 001010101111000001001000111110111111111111
Hexadecimal Output: abc123efff

Decimal to Binary Conversion

Using coden.int_to_bin() function we can convert the decimal number to binary number.

Example

Let’s take a decimal number and convert it to the binary.

import coden
  
decimal_number = 16227
print("Input decimal Number:", decimal_number)

binary_output = coden.int_to_bin(decimal_number)
print('Binary Output',binary_output)

Output

Input decimal Number: 16227
Binary Output 11111101100011

Decimal to Hexadecimal Conversion

Using the int_to_hex() function we can do the conversion of a decimal number to hexadecimal number is possible.

Example

Let’s take a decimal number and convert it the hexadecimal number.

import coden
  
decimal_number = 16227
print("Input decimal Number:", decimal_number)

hexadecimal_output = coden.int_to_hex(decimal_number)
print('Hexadecimal Output',hexadecimal_output)

Output

Input decimal Number: 16227
Hexadecimal Output 3f63

Binary to Decimal Conversion

The coden.bin_to_int() function will perform the Binary to Decimal conversion.

Example

Let’s take a binary number and convert it to the decimal by using the bin_to_int() method.

import coden

binary_number = '001010101111000001001000111110111111111111'
print("Input Binary Number:", binary_number)

decimal_output = coden.bin_to_int(binary_number)
print('Decimal Output:', decimal_output)

Output

Input Binary Number: 001010101111000001001000111110111111111111
Decimal Output: 737679765503

We have discussed all the coden methods related to the code conversions between binary, hexadecimal and decimal numbers.

Updated on: 30-May-2023

75 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements