Binary Conversions

A place for implementation of base conversions

Features

  • To see all the available functions in a module, you can just type help() with the module name as argument. For example,
>>> from pygorithm import binary
>>> help(binary)
    Help on package pygorithm.binary in pygorithm:

    NAME
    pygorithm.binary - Collection or binary conversions and algorithms

    MODULE REFERENCE
    https://docs.python.org/3.5/library/pygorithm.binary.html

    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

    PACKAGE CONTENTS
    ascii
    base10
    base16
    base2
    binary_utils

    DATA
    __all__ = ['ascii', 'base2', 'base10', 'base16']

ASCII Conversions

  • Functions and their uses

ASCII

Conversions from ASCII to:
  • base2
  • base16

Author: Ian Doarn

pygorithm.binary.ascii.to_base16(string, visualize=False)[source]

Convert ascii to hexadecimal :param string: string to convert :param visualize: Show process :param as_string: return value as string not array :return: hex representation of given string

pygorithm.binary.ascii.to_base2(string, visualize=False, as_string=False)[source]

Convert ascii string to binary :param string: Ascii string :param visualize: Show process :param as_string: join strings with a space as one large value :return: array of binary numbers, or entire string

Base2 Coversions

  • Functions and their uses

Binary: Base2

Conversions from base2 to:
  • base10
  • base16
  • ASCII

Author: Ian Doarn

pygorithm.binary.base2.to_ascii(b, visualize=False)[source]

Convert binary to ASCII

Parameters:
  • b – binary number or array
  • visualize – Show process
Returns:

ASCII String

pygorithm.binary.base2.to_base10(n, visualize=False)[source]

Convert given number to a list for every number do the following formula

x * 2 + number

repeat for each result! Example:

binary number = 100110

0 x 2 + 1 = 1 1 x 2 + 0 = 2 2 x 2 + 0 = 4 4 x 2 + 1 = 9 9 x 2 + 1 = 19 19 x 2 + 0 = 38

Parameters:
  • n – binary number
  • visualize – Show process
Returns:

decimal number

pygorithm.binary.base2.to_base16(n, visualize=False)[source]

Convert binary numbers to hexadecimal numbers

Parameters:
  • n – binary number
  • visualize – Visualise the process
Returns:

hexadecimal number

Base10 Coversions

  • Functions and their uses

Binary: Base10

Conversions from base10 to:
  • base2
  • base16

Author: Ian Doarn

pygorithm.binary.base10.to_base2(n, visualize=False)[source]

Divide each number by 2 using the % operator.

Reverse the resulting list of numbers and return the result

Parameters:
  • n – decimal number
  • visualize – Show process
Returns:

binary number

pygorithm.binary.base10.to_base16(n, visualize=False)[source]

Convert decimal number to hexadecimal

Divide the number by 16 and add the remainder to a list, round down the value after division and repeat till our value is 0

Reverse the results list, get each values respective hex value using HEX_VALUES map

Parameters:
  • n – decimal number
  • visualize – Show process
Returns:

hexadecimal number

Base16 Coversions

  • Functions and their uses

Binary: Base16

Conversions from base16 to:
  • base2
  • base10
  • ASCII

Author: Ian Doarn

pygorithm.binary.base16.to_base2(h, visualize=False)[source]

Convert hexadecimal to binary number

Parameters:
  • h – hexadecimal number
  • visualize – Show process
Returns:

binary number

pygorithm.binary.base16.to_base10(h, visualize=False)[source]

Convert hexadecimal number to decimal number

Send hex to a list and reverse. Evaluate each hex value via HEX_LETTER_VALUES map. Enumerate the list,

using the equation: value * 16 ^ index

value is the hex char value: F -> 15 index is its position in the list: [‘1’, ‘A’, ‘F’] F’s index = 2

Continue this for each hex letter until we reach the end of the list, summing all evaluated values.

Parameters:
  • h – hexadecimal number
  • visualize – Show process
Returns:

decimal number

pygorithm.binary.base16.to_ascii(h_array, visualize=False)[source]

Convert base16 array to ASCII string

Input must be a list of strings: Example:

array = [
‘74’, ‘68’, ‘65’, ‘20’, ‘71’, ‘75’, ‘69’, ‘63’, ‘6B’, ‘20’, ‘62’, ‘72’, ‘6F’, ‘77’, ‘6E’, ‘20’, ‘66’, ‘6F’, ‘78’, ‘20’, ‘6A’, ‘75’, ‘6D’, ‘70’, ‘73’, ‘20’, ‘6F’, ‘76’, ‘65’, ‘72’, ‘20’, ‘74’, ‘68’, ‘65’, ‘20’, ‘6C’, ‘61’, ‘7A’, ‘79’, ‘20’, ‘64’, ‘6F’, ‘67’

]

result -> the quick brown fox jumps over the lazy dog

Parameters:
  • h_array – hex value array
  • visualize – Show process
Returns:

ASCII string