Skip to content
Related Articles

Related Articles

Conversion between binary, hexadecimal and decimal numbers using Coden module

View Discussion
Improve Article
Save Article
  • Last Updated : 10 Jul, 2020
View Discussion
Improve Article
Save Article

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




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

Output:

11111101100011

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

Example:

Python3




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

Output:

a5

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

Example:

Python3




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

Output:

34

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

Example:

Python3




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

Output:

18

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

Example:

Python3




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

Output:

1111000111111111

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

Example:

Python3




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

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




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)

Output:

15

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

Example:

Python3




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)

Output:

110

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

Example:

Python3




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

Output:

#63783A

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!