Search code examples
dartbinarytypeconverter

How to convert two 4-bit chunk into 1 byte in Dart?


byte 0: min_value (0-3 bit)
        max_value (4-7 bit)

The byte0 should be the min and max values combined.

min and max values are both integers (in 0-15 range). I should convert them into 4-bit binary, and combine them somehow? (how?)

E.g.

min_value=2  // 0010
max_value=3  // 0011

The result should be an Uint8, and the value: 00100011


Solution

  • You can use the shift left operator << to get the result you want:

    result = ((min_value << 4) + max_value).toRadixString(2).padLeft(8, '0');