As the questions say, how do I achieve this?
If I have a code like this:
let a = "29"; for c in a.chars() { println!("{}", c as u32); }
What I get are the unicode code pages for 2 and 9:
I want these characters to be displayed in actual numbers.
char::to_digit(radix)doing this. radixstands for "base", i.e. 10 for decimal, 16 for hex, etc .:
char::to_digit(radix)
radix
let a = "29"; for c in a.chars() { println!("{:?}", c.to_digit(10)); }
He returns Option, so you need unwrap()it, or better expect("that no number!"). You can learn more about correct error handling in the relevant chapter of Rust .
Option
unwrap()
expect("that no number!")
Well, you can always use the following hacker solution:
fn main() { let a = "29"; for c in a.chars() { println!("{}", c as u32 - 48); } }
ASCII 48 57, , , 2 9, , 50 57. , 48 .
Source: https://habr.com/ru/post/1665234/More articles:Regular expressions with scanf in C - cUnable to change laravel application name - phpJS string: replace with regex index - javascriptWrong factory position in fragment at startup - androidWhy is parallel flow slower? - javaCMake no CMAKE_C_COMPILER error can be found using Xcode and GLFW - c ++R. To get a better overview of the data - rПропускать входящие заявки - adfsЗапустите Bokeh на сервере AWS EC2 и получите пустую страницу. Нет сообщений об ошибках - amazon-web-servicesEntity Framework code first adds a restriction for positive numbers - c #All Articles