我正在尝试将 8 位微控制器(PIC)上的 C 整数分解为其 ASCII 等效字符。
例如:
将 982 转换为 '9','8','2'
到目前为止我想出的一切似乎都相当暴力。这是我现在基本上正在做的主要想法:
if( (10 <= n) && (n < 100) ) {
// isolate and update the first order of magnitude
digit_0 = (n % 10);
// isolate and update the second order of magnitude
switch( n - (n % 10) ) {
case 0:
digit_1 = 0;
break;
case 10:
digit_1 = 1;
break;
...
然后我有另一个函数将 0b00110000 (48 十进制)添加到我的每个数字上。
我一直很难找到任何 C 函数来为我做这件事或自己做好这件事。
预先感谢您的任何帮助!
If sprintf
由于某种原因不适合,像这样简单的事情就可以工作:
char digits[MAX_DIGITS];
int count = 0;
do {
digits[count++] = n % 10;
n /= 10;
} while (n > 0);
会有count
数字,与digits[0]
是最不重要的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)