Skip to content
Related Articles

Related Articles

Conversion between binary, hexadecimal and decimal numbers using Coden module
  • Last Updated : 10 Jul, 2020

Coden is a library based on codes, secret codes. This library can be used for encoding and decoding so that a message can be sent secretly. It can also be used for conversion between binary, hexadecimal and decimal numbers in Python.

Installation

This module does not comes built-in with Python. To install it type the below command in the terminal.

pip install coden

Conversion between binary, hexadecimal and decimal numbers

Decimal to Binary: This module provides a built-int method called int_to_bin() for doing the same.

Example:

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

import coden
  
a_decimal_number = 16227
binary_output = coden.int_to_bin(a_decimal_number)
print(binary_output)

chevron_right


Output:

11111101100011

Decimal to Hexadecimal: This module provides a built-int method called int_to_hex() for doing the same.

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import coden
  
a_decimal_number = 165
hexadecimal_output = coden.int_to_hex(a_decimal_number)
print(hexadecimal_output)

chevron_right


Output:

a5

Binary to Hexadecimal: This module provides a built-int method called bin_to_hex() for doing the same.

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import coden
  
a_binary_number = 110100
hexadecimal_output = coden.bin_to_hex(a_binary_number)
print(hexadecimal_output)

chevron_right


Output:

34

Binary to Decimal: This module provides a built-int method called bin_to_int() for doing the same.



Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import coden
  
a_binary_number = 10010
decimal_output = coden.bin_to_int(a_binary_number)
print(decimal_output)

chevron_right


Output:

18

Hexadecimal to Binary: This module provides a built-int method called hex_to_bin() for doing the same.

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import coden
  
a_hexadecimal_number = "f1ff"
binary_output = coden.hex_to_bin(a_hexadecimal_number)
print(binary_output)

chevron_right


Output:

1111000111111111

Hexadecimal to Decimal: This module provides a built-int method called hex_to_int() for doing the same.

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import coden
  
a_hexadecimal_number = "ffea1a"
decimal_output = coden.hex_to_int(a_hexadecimal_number)
print(decimal_output)

chevron_right


Output:

16771610

Random Numbers using coden module

Random hexadecimal: The function randoms.hexadecimal returns a random hexadecimal number between the given range of hexadecimal numbers.

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import coden
  
a = "f"
b = "1e"
  
# returns random hexadecimal in range 
# of a - b.
random_hexadecimal_output = coden.randoms.hexadecimal(a, b)
print(random_hexadecimal_output)

chevron_right


Output:

15

Random binary: The function randoms.binary returns a random binary number between the given range of binary numbers.

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import coden
  
a = 101
b = 10101
  
# returns random binary in range 
# of a - b.
random_hexadecimal_output = coden.randoms.binary(a, b)
print(random_hexadecimal_output)

chevron_right


Output:

110

Random Hexadecimal Color: randoms.hexcolor() returns a random hexadecimal number(color).

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import coden
  
# returns random hexadecimal color
random_hexadecimal_color_output = coden.randoms.hexcolor()
print(random_hexadecimal_color_output)

chevron_right


Output:

#63783A

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :