Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert UTF-8 hex value to char in Rust?

Tags:

unicode

rust

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)
}
like image 391
ynn Avatar asked Dec 12 '25 11:12

ynn


1 Answers

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("🌸"));
}
like image 114
cdhowie Avatar answered Dec 15 '25 14:12

cdhowie