3 ms·
If I were forced to implement them myself for some reason, I would probably simply do them like this: fn from_be(bytes: [u8; 4]) -> u32 { (bytes[0]
by Measter 5y ago
If I were forced to implement them myself for some reason, I would probably simply do them like this:
fn from_be(bytes: [u8; 4]) -> u32 {
(bytes[0] as u32) << 24
| (bytes[1] as u32) << 16
| (bytes[2] as u32) << 8
| (bytes[3] as u32) << 0
}
It's direct, to the point, and does exactly what it says on the tin because all pertinent behaviour is defined. The way Rust's corelib implements it is to transmute the array into the integer, then call the bswap intrinsic if the bytes need swapping(detected at compile time).