C语言 如何将无符号长整型转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2709713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert unsigned long to string
提问by Walidix
In the C language, how do I convert unsigned longvalue to a string (char *) and keep my source code portable or just recompile it to work on other platform (without rewriting code?
在 C 语言中,如何将unsigned long值转换为字符串(char *)并保持我的源代码可移植或只是重新编译它以在其他平台上工作(无需重写代码?
For example, if I have sprintf(buffer, format, value), how do I determine the size of buffer with platform-independent manner?
例如,如果我有sprintf(buffer, format, value),我如何以独立于平台的方式确定缓冲区的大小?
回答by jfs
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] == 'char *ultostr(unsigned long value, char *ptr, int base)
{
unsigned long t = 0, res = 0;
unsigned long tmp = value;
int count = 0;
if (NULL == ptr)
{
return NULL;
}
if (tmp == 0)
{
count++;
}
while(tmp > 0)
{
tmp = tmp/base;
count++;
}
ptr += count;
*ptr = 'char buffer [50];
unsigned long a = 5;
int n=sprintf (buffer, "%lu", a);
';
do
{
res = value - base * (t = value / base);
if (res < 10)
{
* -- ptr = '0' + res;
}
else if ((res >= 10) && (res < 16))
{
* --ptr = 'A' - 10 + res;
}
} while ((value = t) != 0);
return(ptr);
}
');
assert(c == n);
回答by Carl Smotricz
The standard approach is to use sprintf(buffer, "%lu", value);to write a string rep of valueto buffer. However, overflow is a potential problem, as sprintfwill happily (and unknowingly) write over the end of your buffer.
标准的做法是用sprintf(buffer, "%lu", value);写的字符串代表value到buffer。然而,溢出是一个潜在的问题,因为它sprintf会很高兴(并且在不知不觉中)写到缓冲区的末尾。
This is actually a big weakness of sprintf, partially fixed in C++ by using streams rather than buffers. The usual "answer" is to allocate a very generous buffer unlikely to overflow, let sprintf output to that, and then use strlen to determine the actual string length produced, calloc a buffer of (that size + 1) and copy the string to that.
这实际上是 sprintf 的一大弱点,通过使用流而不是缓冲区在 C++ 中部分修复。通常的“答案”是分配一个不太可能溢出的非常大的缓冲区,让 sprintf 输出到那个缓冲区,然后使用 strlen 来确定产生的实际字符串长度, calloc 一个(那个大小 + 1)的缓冲区并将字符串复制到那个.
This sitediscusses this and related problems at some length.
该站点详细讨论了这个问题和相关问题。
Some libraries offer snprintfas an alternative which lets you specify a maximum buffer size.
一些库提供snprintf了替代方案,可让您指定最大缓冲区大小。
回答by user2083050
you can write a function which converts from unsigned long to str, similar to ltostr library function.
您可以编写一个从 unsigned long 转换为 str 的函数,类似于 ltostr 库函数。
unsigned long x=1000000;
char buffer[21];
sprintf(buffer,"%lu", x);
you can refer to my blog herewhich explains implementation and usage with example.
回答by Jeffrey Vandenborne
#include <stdio.h>
int main ()
{
unsigned long lval = 123;
char buffer [50];
sprintf (buffer, "%lu" , lval );
}
回答by MAK
Try using sprintf:
尝试使用sprintf:
Edit:
编辑:
Notice that you have to allocate a buffer in advance, and have no idea how long the numbers will actually be when you do so. I'm assuming 32bit longs, which can produce numbers as big as 10 digits.
请注意,您必须提前分配缓冲区,并且不知道这样做时数字实际会持续多长时间。我假设是 32bit longs,它可以产生大到 10 位的数字。
See Carl Smotricz's answer for a better explanation of the issues involved.
有关所涉及问题的更好解释,请参阅 Carl Smotricz 的回答。

