簡體   English   中英

為什么 sprintf 只轉換了我的 uint64_t 數據的一半?

[英]Why is the sprintf converting only half of my uint64_t data?

#include <iostream>



using namespace std;

int main()
{
    uint64_t a = 0xffffffffffffffff;
    int c = 3;
    char b[16];
    sprintf(b, "%X", a);
    cout << b << endl;
    return 0;
}

這是打印和存儲一半的 fs,我需要其中的 rest,我該怎么做?

您通過傳遞具有錯誤類型的數據來調用未定義的行為

以大寫十六進制打印uint64_t的類型格式是PRIX64 ,在 header <cinttypes>中定義。

也不要忘記為終止空字符分配。

#include <iostream>
#include <cstdio>    // sprintf()
#include <cinttypes> // PRIX64 (and its family) is defined in this header



using namespace std;

int main()
{
    uint64_t a = 0xffffffffffffffff;
    int c = 3;
    char b[17]; // allocate also for terminating null-character
    sprintf(b, "%" PRIX64, a); // use correct format
    cout << b << endl;
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM