diive.get_encoded_value_from_int

diive.get_encoded_value_from_int(integer, bit_start, bit_end, gain=1, base=2, n_bits=8)[source]

Extracts a subrange of bits from an integer, converts it to a float with an optional gain applied.

Can be used to extract additional variables from a binary value that was stored as an integer.

For example, using integer=18 first converts the integer to its binary representation (‘10010’, without the leading ‘0b’), then adds leading zeros to fulfill the number of bits (n_bits=8, yields ‘00010010’), then extracts the required bit subrange (‘0010’ for bit_start=4 and bit_end=8), then converts the subrange to integer representation (2) and then applies gain (in this example 6.25) to get the final value of 12.5.

Background: Some measurements are stored as binary values that were converted to integers for practical reasons. For example, in the binary value ‘10110001 00101110’ (2 Bytes) each bit can represent a flag that is raised when certain conditions are met. Sometimes it is necessary to extract multiple specific bits from such binary values and convert them to a decimal representation for further processing.

Parameters:
  • integer (int) – The integer value from which to extract bits.

  • bit_start (int) – The starting index of the bit subrange (inclusive).

  • bit_end (int) – The ending index of the bit subrange (exclusive).

  • gain (float) – A gain factor to apply to the converted value (default is 1).

  • base (int) – The base of the integer (default is 2, binary).

  • n_bits (int) – The total number of bits in the integer (default is 8).

Return type:

float

Returns:

The converted and scaled value as a float.

See also

get_encoded_value_series : Apply bit extraction to a series of integers.

Example

See examples/io/io_extract.py for complete examples.