嵌入式 C 中的源代码,用于将无符号整数转换为字符串 [英] Source code in embedded C for unsigned integer to string

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

问题描述

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

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

解决方案

Try this:

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

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 中的源代码,用于将无符号整数转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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