It's just the inverse of the operations from the answer to the question you linked:
fn main() {
// Hex string to 4-bytes, aka. u32
let float: f32 = 18.9;
let bytes = unsafe { std::mem::transmute::<f32, u32>(float) };
let hex = format!("{:x}", bytes);
// Print 41973333
println!("{}", hex);
}
Rust Playground link
Call .from_be()
, .from_le()
or .swap_bytes()
on the u32
value (bytes
) before formatting to alter the byte order. Change from f32
and u32
to f64
and u64
for larger data types.
Similarly, the other answer to that question (using f32.from_bits
) has a direct inverse in f32.to_bits
(though those functions are still marked as unstable).