I have read a lot of ideas how to convert a single char of a string to an integer but I'm struggling with pointers in C. I am guessing at something like:
strftime(buffer, sizeof("0000"), "%H%M", tick_time);
onedigit = atoi(&buffer[1]);
So I want to have the first digit of the time converted into an integer.
If you have a more "elegant" way to do it - let me know.
To get the first character as an integer you want:
onedigit = buffer[0] - '0';
This takes the first (usually ASCII) character, subtracts the (ASCII) value for 0
, leaving you with an integer value in the range 0..9.
You can easily do the same thing for other numeric characters in the string, eg to get the tens digit:
tensdigit = buffer[1] - '0';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.