-
What is the idiomatic rust way to do
u32, f32 <-> [u8;4]
? -
Do you want to do big endian or small endian? Whatever format it’s stored in memory.
Reading out bytes in memory: u32,f32 <-> [u8;4]
zeroexcuses
#1
Well, most direct way would be either an as
cast for u32
and f32
or a std::mem::transmute()
although that needs unsafe code.
TomP
#3
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”.
Hyeonu
#4
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 https://doc.rust-lang.org/std/primitive.usize.html#method.to_be_bytes
zeroexcuses
#5
Does this require unstable library features ‘int_to_from_bytes’ or is there another way to use it?