2010-10-18 147 views
3

我嘗試很長很長的無符號轉換爲字符串這樣無符號長長字符串轉換

unsigned long long Data = 12; 
char Str[20]; 

sprintf(Str, "%lld",Data); 

時,我想看看 但我總是看到00

Str[0],Str[1]....; 

什麼錯! !

+0

你確定「%lld」有效嗎?我只在文檔中看到「%ld」。 – leppie 2010-10-18 11:02:20

+1

就我所見,這是有效的代碼(只有你應該使用'u'而不是'd')。給我們更多的信息,說明你真正做了什麼,以及出了什麼問題。 – 2010-10-18 11:43:00

+0

如果這是針對PIC的,那麼針對無符號long long的sprintf支持最好是粗略的,問題出在庫文件中。 – Graham 2012-08-14 00:39:58

回答

4

在大多數情況下,%llu應該做的伎倆。但是您可能必須在某些Windows平臺上使用%I64u

+1

OP可能沒有使用Windows平臺......使用USART_transmit('x');可能是PIC或Atmega平臺。 – 2010-10-18 11:43:26

3

%lld用於簽字long long,用%llu代替。

1

%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); 
}