let s = "sato".to_string().into_bytes(); println!("{:?}", s); let l = LittleEndian::read_u32(&s); println!("{}", l); let b = BigEndian::read_u32(&s); println!("{}", b); let mut wtr = vec![]; wtr.write_u32::<LittleEndian>(l).unwrap(); println!("{:?}", wtr);
[115, 97, 116, 111]
1869898099
1935766639
[115, 97, 116, 111]
構造体をバイト列にする
#[derive(Serialize, Deserialize, Debug)] struct Name { family: Vec<u8>, first: Vec<u8>, } let name1 = Name {family: "sato".to_string().into_bytes(), first: "taro".to_string().into_bytes()}; println!("{:?}", name1); let family = String::from_utf8(name1.family).unwrap(); println!("{}", family);
Name { family: [115, 97, 116, 111], first: [116, 97, 114, 111] }
sato
なるほどー 前処理、後処理が多くなるが、安全性は高まりそう。