Reading out bytes in memory: u32,f32 <-> [u8;4]

  1. What is the idiomatic rust way to do u32, f32 <-> [u8;4] ?

  2. Do you want to do big endian or small endian? Whatever format it's stored in memory.

Well, most direct way would be either an as cast for u32 and f32 or a std::mem::transmute() although that needs unsafe code.

1 Like

The usual terminology is "big endian", "little endian", and sometimes "native endian" to reflect the endianness of the local machine. The terms arise from an historic 1980 essay by Danny Cohen, titled "On holy wars and a plea for peace".

1 Like

Check {i,u}{8,16,32,64,128,size}::{from,to}_{be,le,ne}_bytes() and f{32,64}::{from,to}_bits().

Edit: they're stable since Rust 1.32.0 usize - Rust


Does this require unstable library features 'int_to_from_bytes' or is there another way to use it?