I have a hex value of a Unicode character. How can I convert it to charin Rust?
char::from_u32() didn't work because char didn't seem to contain a hex value:
fn main() {
let code_point: u32 = 0xf09f8cb8; //emoji '🌸'
println!("{}", code_point); //=> 4036988088
let c = '🌸';
println!("{}", c as u32); //=> 127800 (not 4036988088)
}
As others have pointed out, the u32 value is not a code point, but is a UTF-8 byte sequence when viewed as big-endian.
You can convert this value to a string by combining u32::to_be_bytes() with std::str::from_utf8():
fn main() {
let utf8_u32: u32 = 0xf09f8cb8;
let utf8_bytes = utf8_u32.to_be_bytes();
let s = std::str::from_utf8(&utf8_bytes);
assert_eq!(s, Ok("🌸"));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With