当试图将“24”的八进制值读入unsigned int时,我得到0.只是将十进制值作为字符数组传递。使用C++或C++在八进制数中使用sscanf读取字符数组时的问题
我需要最终值作为32位无符号整型。初始值是从二进制文件读入的。
MCVEsimple.cpp
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
char count[4] = "\030\000\000"; // Just "24" works
__uint32_t value;
sscanf(count, "%x", &value);
fprintf(stderr, "%x", value);
return 0;
}
编译和执行使用
$ g++ simple.cpp -g
$ ./a.out
0
$ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
你真正需要的是检查从系统功能的返回值,就像'的sscanf()',如果贴的代码已经检查返回值,很明显,调用'sscanf()'失败。代码中的任何其他问题都是次要的 – user3629249