在C中将unsigned long转换为string [英] Converting unsigned long to string in C

查看:587
本文介绍了在C中将unsigned long转换为string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有人告诉我如何将unsigned long值转换为字符串

(char *)?

在C ++中有一个函数_ultoa所以想在C中使用类似的函数。

问候,

Shivaraj

Hi,
Could any one tell me how to convert a unsigned long value into string
(char *) ?
In C++ there is a function _ultoa so wanted a similar one in C .
Regards,
Shivaraj

推荐答案

bm********@gmail.com 写道:
bm********@gmail.com writes:

可以告诉我如何将unsigned long值转换为字符串

(char *)吗?
Could any one tell me how to convert a unsigned long value into string
(char *) ?



您想要查看格式为%lu的snprintf。如果你不是足够幸运的话,让snprintf使用具有足够大缓冲的sprintf。


-

Ben。

You want to look at snprintf with a format of "%lu". If you are not
lucky enough to have snprintf use sprintf with a large enough buffer.

--
Ben.


bm ******** @ gmail.com 写道:
bm********@gmail.com wrote:



任何人都可以告诉我如何将无符号长值转换为字符串

(字符*)?

在C ++中有一个函数_ultoa所以想在C中使用类似的函数。

问候,

Shivaraj
Hi,
Could any one tell me how to convert a unsigned long value into string
(char *) ?
In C++ there is a function _ultoa so wanted a similar one in C .
Regards,
Shivaraj



sprintf和snprintf会做你想做的事。


< OT>


您可能希望在C ++中使用字符串流,而不是像_ultoa那样使用原始的




< / OT>

sprintf and snprintf will do what you want.

<OT>

You probably want to use string streams in C++, rather than a primitive
like _ultoa.

</OT>


bm ******** @ gmail.com 说:



任何人都可以告诉我如何将unsigned long值转换为str

(char *)?
Hi,
Could any one tell me how to convert a unsigned long value into string
(char *) ?



char *不是字符串。这只是一种指向一种方式。

A char * isn''t a string. It''s just a way of pointing to one.


在C ++中有一个函数_ultoa,所以想在C中使用类似的函数。
In C++ there is a function _ultoa so wanted a similar one in C .



C ++没有这样的功能。几乎可以肯定是你的实现提供的扩展




首先,确保你有足够的存储空间来表示

无符号长值。通常,如果一个字节中的CHAR_BIT位

和unsigned long中的sizeof(无符号长整数)字节,则可以存储在unsigned中的最大

值long将有一个小数

表示不超过(CHAR_BIT * sizeof(unsigned long)+ 2)/ 3,

你需要一个字节为null终止字符,所以我们有:


#include< limits.h>

#include< stdio.h>

int main(无效)

{

unsigned long n = 12345; / *无论* /

char s [(CHAR_BIT * sizeof n + 2)/ 3 + 1];

sprintf(s,"%lu",n) ;

printf("字符串值是[%s] \ n",s);

返回0;

}


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http: //万维网。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

C++ has no such function. It is almost certainly an extension provided by
your implementation.

First, make sure you have enough storage for the string representation of
the unsigned long value. In general, if there are CHAR_BIT bits in a byte
and sizeof(unsigned long) bytes in an unsigned long, then the largest
value that can be stored in an unsigned long will have a decimal
representation no longer than (CHAR_BIT * sizeof(unsigned long) + 2) / 3,
and you''ll need a byte for the null terminating character, so we have:

#include <limits.h>
#include <stdio.h>

int main(void)
{
unsigned long n = 12345; /* whatever */
char s[(CHAR_BIT * sizeof n + 2) / 3 + 1];
sprintf(s, "%lu", n);
printf("The string value is [%s]\n", s);
return 0;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


这篇关于在C中将unsigned long转换为string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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