使用 C 或 C++ 以八进制形式使用 sscanf 在 char 数组中发出读取
Issue reading in a char array using sscanf in octal using either C or C++
当尝试将“24”的八进制值读入无符号整数时,我得到 0。只需将十进制值作为 char 数组传递即可。
我需要最终值作为 32 位无符号整数。初始值从二进制文件中读入。
MCVE
simple.cpp
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
char count[4] = "0[=10=]0[=10=]0"; // 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
八进制值030
是一个单个字符,表示ASCII encoding中的特殊"CAN"(取消)控制字符(这是英文字母、数字和标点符号的最常见编码)。
当您使用 scanf
(or its sibling sscanf
) 时,它会尝试读取数字,这些数字在 ASCII 编码中是值 060
到 071
(对于数字 0
到 9
,含)。
为了能够使用 sscanf
来读取数字 24
,您需要两个字符 '2'
和 '4'
。 IE。 "24"
(或"24"
)。
如果你想将单个字符 '0'
转换为整数值 24
,那么只需执行例如
value = count[0];
我假设您在来自二进制文件的记录中获得了 4 个字节,并且您想将这 4 个字节转换为 uint32_t
值。我还假设字节带有预期的字节顺序。
进行转换的最简单(也是最符合标准的方式)是将字节表示复制到变量:
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
char count[4] = "0[=10=]0[=10=]0"; // Just "24" works
__uint32_t value;
memcpy(&value, count, sizeof(value));
fprintf(stderr, "%d", value);
return 0;
}
将return如预期24
。
这是您的代码版本,用于检查对 scanf()
的调用是否有错误
注意:不需要的C++头文件被注释掉了。
//#include <iostream>
//#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main( void )
{
char count[4] = "0[=10=]0[=10=]0"; // Just "24" works
uint32_t value;
if( 1 != sscanf(count, "%x", &value) )
{
fprintf( stderr, "sscanf failed" );
exit( EXIT_FAILURE );
}
// implied else, sscanf successful
printf( "%x", value);
return 0;
}
这是结果输出:
sscanf failed
当尝试将“24”的八进制值读入无符号整数时,我得到 0。只需将十进制值作为 char 数组传递即可。
我需要最终值作为 32 位无符号整数。初始值从二进制文件中读入。
MCVE
simple.cpp
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
char count[4] = "0[=10=]0[=10=]0"; // 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
八进制值030
是一个单个字符,表示ASCII encoding中的特殊"CAN"(取消)控制字符(这是英文字母、数字和标点符号的最常见编码)。
当您使用 scanf
(or its sibling sscanf
) 时,它会尝试读取数字,这些数字在 ASCII 编码中是值 060
到 071
(对于数字 0
到 9
,含)。
为了能够使用 sscanf
来读取数字 24
,您需要两个字符 '2'
和 '4'
。 IE。 "24"
(或"24"
)。
如果你想将单个字符 '0'
转换为整数值 24
,那么只需执行例如
value = count[0];
我假设您在来自二进制文件的记录中获得了 4 个字节,并且您想将这 4 个字节转换为 uint32_t
值。我还假设字节带有预期的字节顺序。
进行转换的最简单(也是最符合标准的方式)是将字节表示复制到变量:
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
char count[4] = "0[=10=]0[=10=]0"; // Just "24" works
__uint32_t value;
memcpy(&value, count, sizeof(value));
fprintf(stderr, "%d", value);
return 0;
}
将return如预期24
。
这是您的代码版本,用于检查对 scanf()
注意:不需要的C++头文件被注释掉了。
//#include <iostream>
//#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main( void )
{
char count[4] = "0[=10=]0[=10=]0"; // Just "24" works
uint32_t value;
if( 1 != sscanf(count, "%x", &value) )
{
fprintf( stderr, "sscanf failed" );
exit( EXIT_FAILURE );
}
// implied else, sscanf successful
printf( "%x", value);
return 0;
}
这是结果输出:
sscanf failed