I'm reconstructing u128 numbers passed over the internet. My idea is to break the number down into u8s on one end of the connection, send those and rebuild the u128 on the other end.
I attempted reconstructing a u8 buffer by casting its pointer to *const u8 to *const u128:
fn main() {
let mut b = [0u8; 16];
b[14] = 1;
let addr = &b as *const u8 as *const u128;
unsafe {
println!("{:?}", &b as *const u8);
println!("{:?}\n", b);
println!("{:?}", addr);
println!("n = {}", *addr);
}
}
To my surprise, the generated output looked like this:
0x7efe2088
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
0x7efe2088
n = 5192296858534827628530496329220096
I expected this array to contain
00000000 00000000 ... 00000001 00000000 = 256
Now I'm confused. Considering two different options as possibilities:
- The array is stored backwards, storing
b[14]as the second byte; - A
*const u128(which points tob[0]) reads the allocated bytes backwards.
Which one of these is correct? Is there something I don't understand about the way Rust (or computers in general) store data?