如何为unsigned long转换为字符串 [英] How to convert unsigned long to string
本文介绍了如何为unsigned long转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C语言中,我怎么转换的无符号长值的字符串(的char * ),并保持我的源$ C $ C便携式或只是将其重新编译在其他平台上工作(而无需重写code?
例如,如果我有的sprintf(缓冲,格式,值)
,我怎么确定缓冲与平台无关的方式的大小?
解决方案
const int的N =的snprintf(NULL,0,鲁%,ulong_value);
断言(正大于0);
焦炭BUF [N + 1];
INT C =的snprintf(BUF,N + 1%吕氏春秋,ulong_value);
断言(BUF [N] =='\\ 0');
断言(C == N);
In the C language, how do I convert unsigned long value to a string (char *) and keep my source code portable or just recompile it to work on other platform (without rewriting code?
For example, if I have sprintf(buffer, format, value)
, how do I determine the size of buffer with platform-independent manner?
解决方案
const int n = snprintf(NULL, 0, "%lu", ulong_value);
assert(n > 0);
char buf[n+1];
int c = snprintf(buf, n+1, "%lu", ulong_value);
assert(buf[n] == '\0');
assert(c == n);
这篇关于如何为unsigned long转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文