Created
November 4, 2025 07:24
-
-
Save airled/3089f2a112071364cf64b710b6f4673e to your computer and use it in GitHub Desktop.
Convert u32 to the vec of bytes big-endian
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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