[英]Pebble C convert char to int
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: 我已经读了很多关于如何将字符串的单个char转换为整数的想法,但是我在C语言中的指针方面苦苦挣扎。
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. 这将采用第一个(通常是ASCII)字符,减去(ASCII)值
0
,从而为您提供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';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.