[英]unsigned long long to string convert
我嘗試將unsigned long long轉換為這樣的字符串
unsigned long long Data = 12;
char Str[20];
sprintf(Str, "%lld",Data);
當我想看,但我總是看到00
Str[0],Str[1]....;
什么是錯的!
在大多數情況下, %llu
應該可以解決問題。 但您可能必須在某些Windows平台上使用%I64u
。
%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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.