"if anyone’s in the mood for code..." <- > <@barnabyw:matrix.org> if anyone’s in the mood for code review, I’d be curious to know if there’s a neater way of doing this, which works fine but smells bad > ```rust > /// Convert 12-bit signed integer within a 4 byte long buffer into 16-bit signed integer. > fn a1454_buf_to_i16(buffer: &[u8; 4]) -> i16 { > let lower = buffer[3]; > let mut upper = buffer[2]; > // Fill in additional 1s if the 12 bit number is negative. > if (upper & 0b00001000) == 0b0001000 { > upper = upper | 0b11110000; > } > > let mut sensor_value_raw: u16 = lower.into(); > sensor_value_raw |= (upper as u16) << 8; > let sensor_value: u16 = sensor_value_raw.into(); > let sensor_value = sensor_value as i16; > sensor_value > } > ``` > afaik `from_*e_bytes` won’t work as I need to check the sign of the 12-bit number inside the buffer, and extend the leading 1s if it’s negative I have this little hack function, after you've unpacked the bytes ```rust /// Fix the sign on signed 24-bit and 12-bit integers, represented as `i32`. /// See section 8.11 in the datasheet for the algorithm. /// (Here, we use this for pressure and temp readings) fn fix_int_sign(val: &mut i32, num_bits: u8) { if *val > (1 << (num_bits - 1)) - 1 { *val -= 1 << (num_bits as i32); } ``` I do not love dealing with non-rust-std signed ints