Skip to content

Instantly share code, notes, and snippets.

@airled
Created November 4, 2025 07:24
Show Gist options
  • Select an option

  • Save airled/3089f2a112071364cf64b710b6f4673e to your computer and use it in GitHub Desktop.

Select an option

Save airled/3089f2a112071364cf64b710b6f4673e to your computer and use it in GitHub Desktop.
Convert u32 to the vec of bytes big-endian
fn int_to_bytes(num: u32) -> Vec<u8> {
let mut bytes: Vec<u8> =
(0..4).map(|offset| ((num >> offset * 8) & 0xFF) as u8).collect();
bytes.reverse();
bytes
}
fn main() {
let res = int_to_bytes(1234u32);
println!("{:?}", res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment