Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert ascii char to int like c/c++ in rust

In c/c++, an ascii char can be converted to a number

int('a')

But how to do this in rust?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can simply convert character to u32 this way: let code = 'a' as u32;. This will give you unicode value for specific character.

fn main() {
    let s = "0123";
    for c in s.chars() {
        println!("{c} -> {}", c as u32);
    }
}

Try it here

But if you are strictly require to work with ASCII not with Unicode, you can also check ascii crate

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading