std-method

Floating-point byte encoding

Serialize a binary float using the agreed width and order, retaining the encoding rather than generating decimal text. Expected stdout (not executed): [63, 248, 0, 0, 0, 0, 0, 0] 1.5 Common mistake: The reader must know whether the value is f32 or f64, as well as byte order.

Formal Syntax & Structure

rust syntaxrust
x.to_be_bytes(); f64::from_be_bytes(bytes); x.to_le_bytes(); f64::from_le_bytes(bytes); x.to_ne_bytes(); f64::from_ne_bytes(bytes)

Working Example

Example Snippetrust
fn main() {    let x = 1.5_f64;    println!("{:?}", x.to_be_bytes());    println!("{}", f64::from_le_bytes(x.to_le_bytes()));}
Rust Reference