简体   繁体   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;
}

This is printing and storing half of the fs, i need the rest of them, how do i do this?这是打印和存储一半的 fs,我需要其中的 rest,我该怎么做?

You are invoking undefined behavior by passing data having wrong type.您通过传递具有错误类型的数据来调用未定义的行为

The type format to print uint64_t in uppercase hex is PRIX64 , defined in the header <cinttypes> .以大写十六进制打印uint64_t的类型格式是PRIX64 ,在 header <cinttypes>中定义。

Also don't forget to allocate for terminating null-character.也不要忘记为终止空字符分配。

#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