#[SOLVED] Converting a string slice to a slice of 4 bytes

6 messages · Page 1 of 1 (latest)

severe spoke
#

Hi all.

Is it possible to convert a &str to a [u8; 4]? My ultimate goal is to implement FromStr for a custom type that wraps [u8; 4], so that it's possible to create an instance of it from a &str.

uncut pawn
#

first use .as_bytes() on the &str to get &[u8]

#

then use .try_into() on that to convert it to [u8; 4] (and fail if the length is wrong)

severe spoke
#

[SOLVED] Converting a string slice to a slice of 4 bytes

grim pumice