我有一个应用程序,其中输入是一组原始字节,我想查看每个字节的两位数十六进制代码。现在我正在尝试获取一个字节的正确十六进制代码。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char hexvc; //= ascii code for hex value
char aval[2]={0xFF,0x00}; //Our raw 1 byte data. can't print actual code here
char hexv[20]; //value for output
memset(hexv,0,19); //clear output
hexvc=strtol(aval,NULL,16) & 0xFF;
if (hexvc != '\0'){
sprintf(hexv,"%02hx",hexvc);
}else{
//hexvc always equals 0. why not FFh?
strcpy(hexv,"00");
}
printf("%s\n",hexv);
return 0;
}
我原本希望看到 ff 出现在屏幕上,但我看到的是 00。 我该如何解决?
如果我将 aval[2]=
之后的 0xFF
更改为 0x31
或 '1'
(因为1 是 ascii 代码 0x31 的实际值),那么我希望看到 31
出现在屏幕上。
最佳答案
aval[2]={0xFF,0x00};
这不是 ascii。
aval[]={'f', 'f',0x00};
或者
aval[]="ff";
然后再试一次
关于c - 从原始字符转换为十六进制等效值时返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56365004/