當試圖將「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