将字符串扫描为十六进制字符数组 [英] scanning a string to hex char array
问题描述
这是我的示例代码:
char a;
char str [20];
unsigned char b[8] ;
unsigned char c[8];
int argsread;
int i;
init_8051();
while(1)
{
printf("
enter a 64 bit operation
");
argsread = scanf("%s", str);
sscanf(str, "0x%x%c0x%x", b, &a, c);
printf("
%d arguments read
",argsread);
for(i=0;i<8;i++)
{
printf("
%#x %c %#x
",b[i],a,c[i]);
}
}
}
这里的问题是,当我在终端中输入例如以下输入时:0x1234567890abcdef+0x1234567890abcdef
The problem here is that when i enter for example in the terminal the following input : 0x1234567890abcdef+0x1234567890abcdef
这会导致错误,其中 char 数组的输出完全错误,并且大部分数字都被跟踪到 a 中,这应该是加号,我在做一些根本错误的事情吗?
this leads to an error where output for char array is totally wrong and most of the numbers are traced into a , which should have been the plus sign , am ai doing something fundamentally wrong ?
更新:我将代码更改为以下内容:
Update: I changed my code to the following :
while(1)
{
printf("
enter a 64 bit operation
");
argsread = scanf("%s", str);
sscanf(str, "0x%s%c0x%s", b, &a, c);
printf("
%d arguments read
",argsread);
printf("
%s
",b);
}
并且我将 str 的大小增加到 30 问题是程序的输出是:
and i increased size of str to 30 the problem is the output of the program is :
1 arguments read
abcdef+0xabcdef
所以 b 的值而不是只是 abcdef 整个字符串!
so the value of b instead of being just abcdef it the the whole string!!
更新 2:发现这段代码很完美,但我想使用 scanf 而不是 cin 这里是代码
Update2: Found this code which works perfect but i wanted to use scanf instead of cin here is the code
:`#include <iostream>
using namespace std;
int main()
{
float a, b, result;
char oper, clear;
cout << "Please enter an equation: i.e 5+5 " << endl;
for (;;) {
cin >> a;
cin >> oper;
cin >> b;
if (oper == '+')
result = a + b;
else if (oper == '-')
result = a - b;
else if (oper == '*')
result = a * b;
else if (oper == '/')
result = a / b;
cout << "= " << result << endl << endl;
cin.clear();
cin.ignore();
}
} `
推荐答案
如果代码以 fgets()
开头,则用户输入和错误处理会更容易.
然后使用sscanf()
、strtol()
等进行解析.
User input and error handling much easier if code starts with fgets()
.
Then use sscanf()
, strtol()
, etc. to parse.
printf("
enter a 64 bit operation
");
char buf[100];
if (fgets(buf, sizeof buf, stdin) == NULL) Handle_IOErrororEOF();
char a;
char b[17]; // use right size arrays
char c[17];
// use width of 16 and %[]
if (sscanf(buf, " 0x%16[0-9abcdef] %c 0x%16[0-9abcdef]", b, &a, c) != 3) {
Handle_Bad_Input();
}
OTOH,只需使用允许十六进制输入 "%x"
或 "%i"
OTOH, just use an integer format specifier that allows hex input "%x"
or "%i"
unsigned long long b,c;
if (sscanf(buf, "%lli %c%lli", &b, &a, &c) != 3) {
Handle_Bad_Input();
}
<小时>
为什么 char str [20];scanf("%s", str);
有问题:
"%s"
做了三件事:
1) 扫描但不保存所有前面的(0 个或多个)空格(' '
, ' '
, '
'
等).
2) 扫描并保存所有非空白.
3)最终达到一个空白.它停止扫描并将空白放回 stdin
.
"%s"
does 3 things:
1) scans, but does not save, all preceding (0 or more) white-space (' '
, ' '
, '
'
, etc.).
2) scans and saves all non-white-space.
3) Finally reaching a white-space. it stops scanning and puts that white-space back into stdin
.
"%s"
说明符缺少宽度,如 "%19s"
,因此很容易溢出 str
The "%s"
specifier lacks a width, like "%19s"
, so it can easily overfill str
sscanf(str, "0x%s%c0x%s", b, &a, c);
也有问题.
输入在第一个数字的末尾和 '+'
之间没有空格,所以 "%s"
继续扫描.代码不检查 sscanf()
的返回值,然后使用 a
、b
、c
.因此 a
、b
、c
可能无法正确扫描或初始化 - 导致潜在的未定义行为.
Input has no white-space between the end of the first number and the '+'
, so "%s"
continues scanning. Code does not check the return value from sscanf()
and then uses a
, b
, c
. So a
, b
, c
may not be properly scanned nor initialized - leading to potential undefined behavior .
这篇关于将字符串扫描为十六进制字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!