std-method

Integer little-endian bytes

Encode a fixed-width field with its least significant byte first when the file format specifies that order. Expected stdout (not executed): [4, 3, 2, 1] true Common mistake: Decoding with a different byte order changes the numeric value.

Formal Syntax & Structure

rust syntaxrust
u32::to_le_bytes(self) -> [u8; 4]; u32::from_le_bytes([u8; 4]) -> u32

Working Example

Example Snippetrust
fn main() {    let bytes = 0x0102_0304_u32.to_le_bytes();    println!("{:?}", bytes);    println!("{}", u32::from_le_bytes(bytes) == 0x0102_0304);}
Rust Reference