扫描字符串为十六进制字符数组 [英] scanning a string to hex char array

查看:191
本文介绍了扫描字符串为十六进制字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的示例code:

 字符一个;
焦海峡[20];
unsigned char型B〔8〕;
无符号字符C [8]。

INT argsread;
INT I;

init_8051();

而(1)
{
    的printf(\ñ输入一个64位运算的\ n);

    argsread = scanf函数(%S,STR);

    sscanf的(STR,0X%X%C0X%X,B&安培; A,C);

    的printf(\ñ%D参数读\ N,argsread);

        对于(i = 0; I< 8;我++)
{
             的printf(\ N%#X%C%#x的\ N,B [I],A,C [I]);
}
        }

}
 

这里的问题是,当我在终端下列输入进入例如: 0x1234567890abcdef + 0x1234567890abcdef

这会导致一个错误的输出字符数组是完全错误的,大部分的数字被追查到,这应该是加号,我ai的做一些根本性的错误?

更新: 我改变了我的code以下内容:

 ,而(1)
{
的printf(\ñ输入一个64位运算的\ n);
argsread = scanf函数(%S,STR);
sscanf的(STR,0X%S%C0X%的,B&安培; A,C);
的printf(\ñ%D参数读\ N,argsread);

的printf(\ñ%S \ N,B);
}
 

和我增加str的大小为30的问题是程序的输出是:

  1个参数读

 ABCDEF + 0xabcdef
 

所以B,而不是仅仅是ABCDEF它的价值在整个字符串!

UPDATE2: 发现这个code这完美的作品,但我想用scanf函数,而不是CIN这里是code

 :`#包括<的iostream>

使用名字空间std;

诠释的main()
{

  漂浮A,B,结果;
    焦炭OPER,清晰;
    COUT<< 请输入一个公式:即5 + 5<< ENDL;
    对于 (;;) {
          CIN>>一个;
          CIN>> OPER;
          CIN>> B:
         如果(OPER =='+')
        结果= A + B;
         否则,如果(OPER ==' - ')
            结果= A  -  B:
       否则,如果(OPER =='*')
              结果= A * B;
     否则,如果(OPER =='/')
              结果= A / B;
    COUT<< =&其中;&其中;结果<< ENDL<< ENDL;
    cin.clear();
    cin.ignore();
   }
}`
 

解决方案

用户输入和错误处理,如果code开头更容易与与fgets()
然后使用的sscanf()与strtol()等进行解析。

 的printf(\ñ输入一个64位运算的\ n);
焦炭BUF [100];
如果(与fgets(buf中,sizeof的BUF,标准输入)== NULL)Handle_IOErrororEOF();

所以char a;
炭B〔17〕; //使用权大小的数组
焦炭C [17];
//为16%和宽度使用[]
如果(sscanf的(BUF,0X%16 [0-9abcdef]%C为0x%16 [0-9abcdef],B&安培; A,C)= 3!){
  Handle_Bad_Input();
}
 

OTOH,只需使用一个整数格式说明,允许十六进制输入%X%I

 无符号长长B,C;
如果(sscanf的(buf中,%LLI%C%LLI,和b,&安培;一,&安培;!C)= 3){
  Handle_Bad_Input();
}
 


为什么字符STR [20]; scanf函数(%S,STR); 有麻烦了:

%的做三件事情:
1)扫描,但不保存,所有的preceding(0或更多)空白('\ T''\ N'等)。
2)扫描并保存所有非空白。
3)最后达到一个空白。它停止扫描并将该空白回标准输入

%的说明缺乏宽度,如%19秒,所以它可以很容易溢出 STR

的sscanf(STR,0X%S%C0X%的,B&安培; A,C); 有太多的麻烦。

输入了第一个号码的结束和之间没有空格+,所以%的继续扫描。 code不检查从​​ sscanf的返回值(),然后使用 A B C 。因此, A B C 可能无法正确​​扫描也不初始化 - 导致潜在的不确定的行为

Here is my sample code :

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]);
}
        }

}

The problem here is that when i enter for example in the terminal the following input : 0x1234567890abcdef+0x1234567890abcdef

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("\n enter a 64 bit operation \n");
argsread = scanf("%s", str);
sscanf(str, "0x%s%c0x%s", b, &a, c);
printf("\n %d arguments read \n",argsread);

printf("\n %s \n",b);
}

and i increased size of str to 30 the problem is the output of the program is :

 1 arguments read 

 abcdef+0xabcdef 

so the value of b instead of being just abcdef it the the whole string!!

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();
   } 
} `

解决方案

User input and error handling much easier if code starts with fgets().
Then use sscanf(), strtol(), etc. to parse.

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, 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();
}


Why char str [20]; scanf("%s", str); has trouble:

"%s" does 3 things:
1) scans, but does not save, all preceding (0 or more) white-space (' ', '\t', '\n ', 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.

The "%s" specifier lacks a width, like "%19s", so it can easily overfill str

sscanf(str, "0x%s%c0x%s", b, &a, c); has trouble too.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆