无符号长长字符串转换
问题描述:
我尝试很长很长的无符号转换为字符串这样无符号长长字符串转换
unsigned long long Data = 12;
char Str[20];
sprintf(Str, "%lld",Data);
时,我想看看 但我总是看到00
Str[0],Str[1]....;
什么错! !
答
在大多数情况下,%llu
应该做的伎俩。但是您可能必须在某些Windows平台上使用%I64u
。
+1
OP可能没有使用Windows平台......使用USART_transmit('x');可能是PIC或Atmega平台。 – 2010-10-18 11:43:26
答
%lld
用于签字long long
,用%llu
代替。
答
%llu应该像添加到标准一样工作。不过,您可以使用安全版本的snprintf或考虑编写比snprintf更好的函数。这里是你可能感兴趣的一个。
char *ulltostr(uint64 value, char *ptr, int base)
{
uint64 t = 0, res = 0;
uint64 tmp = value;
int count = 0;
if (NULL == ptr)
{
return NULL;
}
if (tmp == 0)
{
count++;
}
while(tmp > 0)
{
tmp = tmp/base;
count++;
}
ptr += count;
*ptr = '\0';
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);
}
你确定“%lld”有效吗?我只在文档中看到“%ld”。 – leppie 2010-10-18 11:02:20
就我所见,这是有效的代码(只有你应该使用'u'而不是'd')。给我们更多的信息,说明你真正做了什么,以及出了什么问题。 – 2010-10-18 11:43:00
如果这是针对PIC的,那么针对无符号long long的sprintf支持最好是粗略的,问题出在库文件中。 – Graham 2012-08-14 00:39:58