来源$ C ​​$ C在嵌入式C为无符号整数串 [英] Source code in embedded C for unsigned integer to string

查看:134
本文介绍了来源$ C ​​$ C在嵌入式C为无符号整数串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有求助于标准库utoa,我在寻找utoa来源$ C ​​$ C,所以我可以自定义为特定的项目。我有输出无符号整数(32位),如0xFFFF_FFFF

Without resorting to standard library utoa, I'm looking for source code of utoa so I may customise it for specific project. I have unsigned integer (32 bits) with output such as 0xFFFF_FFFF

我也想找源$ C ​​$ C为二进制格式的无符号整数,半字字符串。

I also looking for source code for unsigned integer and half word to string in binary format.

推荐答案

试试这个:

char *dec(unsigned x, char *s)
{
    *--s = 0;
    if (!x) *--s = '0';
    for (; x; x/=10) *--s = '0'+x%10;
    return s;
}

您的指针调用者提供缓冲区的结束调用它,并且函数返回的指针字符串的开头。缓冲区应该有长度至少为 3 * sizeof的(INT)+1 是安全的。

You call it with a pointer to the end of a caller-provided buffer, and the function returns the pointer to the beginning of the string. The buffer should have length at least 3*sizeof(int)+1 to be safe.

当然,这很容易适应其他基地。

Of course this is easily adapted to other bases.

这篇关于来源$ C ​​$ C在嵌入式C为无符号整数串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆