C 将字符串扫描为十六进制字符数组

C 将字符串扫描为十六进制字符数组,c,string,hex,64-bit,scanf,C,String,Hex,64 Bit,Scanf,以下是我的示例代码: char a; char str [20]; unsigned char b[8] ; unsigned char c[8]; int argsread; int i; init_8051(); while(1) { printf("\n enter a 64 bit operation \n"); argsread = scanf("%s", str); sscanf(str, "0x%x%c0x%x", b, &a, c);

以下是我的示例代码:

char a;
char str [20];
unsigned char b[8] ;
unsigned char c[8];

int argsread;
int i;

init_8051();

while(1)
{
    printf("\n enter a 64 bit operation \n");

    argsread = scanf("%s", str);

    sscanf(str, "0x%x%c0x%x", b, &a, c);

    printf("\n %d arguments read \n",argsread);

        for(i=0;i<8;i++)
{
             printf("\n %#x %c %#x\n",b[i],a,c[i]);
}
        }

}
我将str的大小增加到30,问题是程序的输出是:

 1 arguments read 

 abcdef+0xabcdef 
所以b的值不是abcdef而是整个字符串的值

更新2: 我发现这段代码非常好用,但是我想用scanf而不是cin

:`#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();
   } 
} `
:`include
使用名称空间std;
int main()
{
浮动a、b、结果;
字符操作,清晰;
库塔;
cin>>操作;
cin>>b;
如果(操作=='+')
结果=a+b;
else if(oper=='-')
结果=a-b;
else if(oper=='*')
结果=a*b;
else if(oper=='/'))
结果=a/b;

cout您正在使用scanf读取字符串并将其存储在整数中。我认为这就是导致问题的原因

另外,从
MAN
页面

AME
       scanf,  fscanf, sscanf, vscanf, vsscanf, vfscanf 

       ...

RETURN VALUE

   These functions return the number of input items  successfully  matched
   and assigned, which can be fewer than provided for, or even zero in the
   event of an early matching failure.

   The value EOF is returned if the end of input is reached before  either
   the  first  successful conversion or a matching failure occurs.  EOF is
   also returned if a read error occurs, in which case the error indicator
   for  the  stream  (see ferror(3)) is set, and errno is set indicate the
   error.

我认为你错放了作业,如果你想知道有多少个参数匹配,那么就改变这个

argsread = scanf("%s", str);

sscanf(str, "0x%x%c0x%x", b, &a, c);
对此

scanf("%s", str);

argsread = sscanf(str, "%x%c%x", b, &a, c);
根据你的意见,你应该改变一些其他的事情,可能是这个代码将工作

char a;
char str [64];
unsigned int b;
unsigned int c;
char B[11];
char C[11];
int argsread;

while(1)
{
    printf("\n enter a 64 bit operation \n");

    scanf("%s", str);

    argsread = sscanf(str, "0x%x%c0x%x", &b, &a, &c);
    if (argsread == 3) 
    {
        printf("\n %d arguments read\n", argsread);

        snprintf(B, sizeof(B), "0x%08X", b);
        snprintf(C, sizeof(B), "0x%08X", c);

        printf("\n %s%c%s\n", B, a, C);
    }
}
return 0;

如果代码以
fgets()开头,则用户输入和错误处理将更加容易
然后使用
sscanf()
strtol()
等进行解析

printf("\n enter a 64 bit operation \n");
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”


为什么
char str[20];scanf(“%s”,str);
有问题:

“%s”
做3件事:
1) 扫描但不保存前面的所有(0或更多)空白(
'
'\t'
'\n'
等)。
2) 扫描并保存所有非空白。
3) 最后到达一个空白。它停止扫描并将该空白放回
stdin

“%s”
说明符缺少宽度,如
“%19s”
,因此它很容易填充过多
str

sscanf(str,“0x%s%c0x%s”,b和a,c);
也有问题


输入在第一个数字的结尾和
'+'
之间没有空格,因此
“%s”
继续扫描。code不检查
sscanf()的返回值
然后使用
a
b
c
。因此
a
b
c
可能未正确扫描或初始化-导致潜在的未定义行为。

您的代码真的是这样格式化的还是“没有时间”为我们格式化它?我只是写了10分钟,因为我正在试图弄清楚如何做,我不先格式化,但我会为你做:)只是一个友好的建议-尝试从一开始就编写格式化的代码,而不是以后重新格式化。这节省了时间,使查找错误/bug变得更容易。当然,我确实格式化了一点位可读,感谢
0x1234567890abcdef+0x1234567890abcdef
超过20个
chars
s。因此此
argsread=scanf(“%s”,str);
overflows
str
。我将其更改为添加到字符串中,但仍然存在根据0x和加号将字符串拆分为子字符串的问题OK,但这不是问题所在,问题在于“0x%x%c0x%x”正如您在我的更新中所看到的,b数组接受所有输入,并且它不受0x的限制,因为我的输入字符串应该分为3个子字符串2以0x开头,中间是符号操作我需要在字符数组中选择它们,所以我将其改为“0x%s%c0x%s”但是它仍然不起作用,请参见更新。ThanksA字符串是一个以空结尾的
char
数组,因此中间的
char
将被视为字符串的一部分,而
scanf
将消耗整个输入,因此返回
1
@Omarshaaban检查我的新答案更新,这对您有用吗。小调:
“0x”
“0x%x…”中的
不需要。
“%x”
将使用前导的“0x”。
printf("\n enter a 64 bit operation \n");
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();
}
unsigned long long b,c;
if (sscanf(buf, "%lli %c%lli", &b, &a, &c) != 3) {
  Handle_Bad_Input();
}