convert - Functions to convert between low-level types (binary, hex, ascii and int)

This module provides general low-level conversion functions such as:

  • ascii -> binary

  • ascii -> hexidecimal

  • ascii -> integer

  • integer -> ascii

  • integer -> binary

  • integer -> hexidecimal

  • etc.

Purpose:

This module contains various low-level conversion functions.

Platform:

Linux/Windows | Python 3.7+

Developer:

J Berendt

Email:

development@s3dev.uk

Comments:

Often, these types of conversions are called from a high-iteration loop. Therefore, the implementation of these functions has been written as close to core Python as possible, or in a C style, for efficiency purposes.

convert.ascii2bin(asciistring: str) str[source]

Convert an ASCII string into a binary string representation.

Parameters:

asciistring (str) – ASCII string to be converted.

Returns:

A binary string representation for the passed ASCII text, where each ASCII character is represented by an 8-bit binary string.

Return type:

str

convert.ascii2hex(asciistring: str) str[source]

Convert an ASCII string into a hexidecimal string.

Parameters:

asciistring (str) – ASCII string to be converted.

Returns:

A hexidecimal string representation of the passed ASCII text.

Return type:

str

convert.ascii2int(asciistring: str) list[source]

Convert an ASCII string to a list of integers.

Parameters:

asciistring (str) – ASCII string to be converted.

Returns:

A list of integers, as converted from he ASCII string.

Return type:

list

convert.bin2ascii(binstring: str, bits: int = 8) str[source]

Convert a binary string representation into ASCII text.

Parameters:
  • binstring (str) – Binary string to be converted.

  • bits (int, optional) – Bit chunks into which the binary string is broken for conversion. Defaults to 8.

Returns:

An ASCII string representation of the passed binary string.

Return type:

str

convert.bin2hex(binstring: str, bits: int = 8) str[source]

Convert a binary string representation into a hex string.

Parameters:
  • binstring (str) – Binary string to be converted.

  • bits (int, optional) – Bit chunks into which the binary string is broken for conversion. Defaults to 8.

Returns:

A hexidecimal string representation of the passed binary string.

convert.bin2int(binstring: str, bits: int = 8) int[source]

Convert a binary string representation into an integer.

Parameters:
  • binstring (str) – Binary string to be converted.

  • bits (int, optional) – Bit chunks into which the binary string is broken for conversion. Defaults to 8.

Returns:

Integer value from the binary string.

Return type:

int

convert.hex2ascii(hexstring: str) str[source]

Convert a hexidecimal string to ASCII text.

Parameters:

hexstring (str) – Hex string to be converted.

Returns:

An ASCII string representation for the passed hex string.

Return type:

str

convert.hex2bin(hexstring: str) str[source]

Convert a hexidecimal string into a binary string representation.

Parameters:

hexstring (str) – Hex string to be converted.

Returns:

A binary string representation of the passed hex string.

Return type:

str

convert.hex2int(hexstring: str, nbytes: int = 1) int[source]

Convert a hexidecimal string to an integer.

Parameters:
  • hexstring (str) – Hex string to be converted.

  • nbytes (int, optional) – Number of bytes to consider for each decimal value. Defaults to 1.

Examples:

Example usage:

hex2int(hexstring='c0ffee', nbytes=1)
>>> [192, 255, 238]

hex2int(hexstring='c0ffee', nbytes=2)
>>> [49407, 238]

hex2int(hexstring='c0ffee', nbytes=3)
>>> [12648430]
Returns:

A list of decimal values, as converted from the hex string.

Return type:

list

convert.int2ascii(i: int) str[source]

Convert an integer to an ASCII character.

Parameters:

i (int) – Integer value to be converted to ASCII text.

Note

The passed integer value must be <= 127.

Raises:

ValueError – If the passed integer is > 127.

Returns:

The ASCII character associated to the passed integer.

Return type:

str

convert.int2bin(i: int) str[source]

Convert an 8-bit integer to a binary string.

Parameters:

i (int) – Integer value to be converted.

Note

The passed integer value must be <= 255.

Raises:

ValueError – If the passed integer is > 255.

Returns:

A binary string representation of the passed integer.

Return type:

str

convert.int2hex(i: int) str[source]

Convert an integer into a hexidecimal string.

Parameters:

i (int) – Integer value to be converted.

Returns:

A two character hexidecimal string for the passed integer value.

Return type:

str