bin6_four demonstrates how to create 4 byte pseudobinary encoded numbers

bin6_four.py

bin6_four.bin6_four(num, count=4, right_digits=0, is_signed=True)

Converts a number in to a count byte long 6-bit packed binary string (used for GOES formatting). The maximum number of bytes is 4, which allows a representation range of [-8388608 .. 8388607]. The more right_digits you request, the smaller the range. For instance with 2 right_digits, the the maximum range becomes limited to [-1310.72 .. 1310.71], and even less with fewer bytes.

Examples:

>>> # Convert 1234567 to 6-bit pseudo-binary:
>>> bin6_four(1234567)
b'DmZG'
>>> # Convert 12345 to 6-bit pseudo-binary:
>>> bin6_four(12345, 3)
b'C@Y'
>>> # Convert BV in to a 3-byte 18-bit value:
>>> BV = 12.32
>>> bin6_four(BV, 3, 2)
b'@SP'
Parameters:
  • num (float) – A number to convert to pseudo-binary format.
  • count (int) – The number of bytes to format (1 to 3).
  • right_digits (int) – The number of decimal digits to retain via scaling.
  • is_signed (bool) – True if negative values should be allowed.
Returns:

A byte string containing 6-bit binary data.

Return type:

bytes