fix endianess

This commit is contained in:
themighty1
2022-04-16 07:39:29 +03:00
parent fc831a7559
commit e3d37099da

View File

@@ -5,7 +5,7 @@ pub fn boolvec_to_u8vec(bv: &[bool]) -> Vec<u8> {
let offset = if bv.len() % 8 == 0 { 0 } else { 1 };
let mut v = vec![0u8; bv.len() / 8 + offset];
for (i, b) in bv.iter().enumerate() {
v[i / 8] |= (*b as u8) << (i % 8);
v[i / 8] |= (*b as u8) << (7 - (i % 8));
}
v
}
@@ -15,7 +15,7 @@ pub fn u8vec_to_boolvec(v: &[u8]) -> Vec<bool> {
let mut bv = Vec::with_capacity(v.len() * 8);
for byte in v.iter() {
for i in 0..8 {
bv.push((1 << i) & byte != 0);
bv.push(((byte >> (7 - i)) & 1) != 0);
}
}
bv
@@ -84,6 +84,10 @@ mod tests {
#[test]
fn test_boolvec_to_u8vec() {
let mut u = vec![false; 16];
u[7] = true;
assert_eq!(boolvec_to_u8vec(&u), &256u16.to_be_bytes());
let v = (0..128)
.map(|_| rand::random::<bool>())
.collect::<Vec<bool>>();
@@ -94,6 +98,10 @@ mod tests {
#[test]
fn test_u8vec_to_boolvec() {
let mut u = vec![false; 16];
u[7] = true;
assert_eq!(u8vec_to_boolvec(&256u16.to_be_bytes()), u);
let v = (0..128).map(|_| rand::random::<u8>()).collect::<Vec<u8>>();
let v_ = u8vec_to_boolvec(&v);
let v__ = boolvec_to_u8vec(&v_);