Byteorder - how to take a bool-value?

Hello.
I have a problem with decoding data.
Could I have taken a bool-value by Cursor?
How to take a bool-value from Cursor<&Vec<u8>>?

My code: Playground

My function: pub fn decode_data(data_type: &MyDataType, cursor: &mut Cursor<&Vec<u8>>, byte_order: &ByteOrdering) -> MyValue should return decoded value.

I use the library:
https://crates.io/crates/byteorder

You can read a u8, and treat 0 as false and anything else as true:

MyValue::BOOL(cursor.read_u8().unwrap() != 0)

(this is assuming you control the input format)

It's not working correctly.
Check this: Playground

I need to move bit by bit (DataType).

We'd need to know the details of how the data is encoded. Does it pack multiple bools into a single byte, and if so does it start from the most-significant or least-significant bit? And are any padding bits inserted if the next value is not a BOOL? (If not, then following values might not be byte-aligned, which means you can't read them using byteorder.)

Your data_for_decoding is eight bytes long, and you are trying to read eight BOOL values followed by one U8 value. I would expect the data to be either two bytes long (if each BOOL takes up one bit) or nine bytes long (if each BOOL takes up one byte), not eight.

1 Like

We'd need to know the details of how the data is encoded. Does it pack multiple bools into a single byte, and if so does it start from the most-significant or least-significant bit?

Ultimately, it should be configurable. Suppose Lsb0.

let data_for_decoding: Vec<u8> = vec![0b00000001, 0, 0, 0, 0, 0, 0, 0];
// 0b00000001 => MyDataType::BOOL / MyValue(true)

And are any padding bits inserted if the next value is not a BOOL ? (If not, then following values might not be byte-aligned, which means you can't read them using byteorder .)

Yes.
For example:

let data_types = vec![
                    MyDataType::BOOL, // 0
                    MyDataType::BOOL, // 1
                    MyDataType::BOOL, // 2
                    MyDataType::BOOL, // 3
                    MyDataType::BOOL, // 4
                    MyDataType::BOOL, // 5
                    // 6 = None = Padding
                    // 7 = None = Padding
                    MyDataType::U8,
                   // ...
         ];

The vector's (data_for_decoding) size is 8 because is a can_data (fixed size).

In this case, you'll need more state than just a Cursor<Vec<u8>>, since you will also need to keep track sometimes of your current bit offset within the current byte.

If you know which bit to read next, you can use bit shifting and masking to retrieve it and convert it to bool, e.g.:

fn get_bit_lsb0(x: u8, bit_offset: u32) -> bool {
    x & (1 << bit_offset) != 0
}

You should advance the cursor only after reading all eight bits from a byte, or when the next data type is not BOOL.

1 Like

@mbrubeck @Kestrer
Check this

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.