比如说有个字符型数组,str[0]='6',str[1]='8',如何将这两个字符转换成一个十六进制类型68放在一个字符型数组的buf[0]中
就剩5分了,兄弟姐妹们帮帮忙,非常感谢!
最好是用c语言,谢谢
第1个回答 推荐于2016-02-27
#include <stdio.h>
void main(void)
{
unsigned char i,str[2],temp[2],buf[1];
str[0] = '6';
str[1] = '8';
for(i=0;i<2;i++)
{
if(str[i]>='0' && str[i]<='9') temp[i]=str[i]-'0';
else if(str[i]>='A' && str[i]<='F') temp[i]=str[i]-'A'+0x0A;
else if(str[i]>='a' && str[i]<='f') temp[i]=str[i]-'a'+0x0a;
else
{
printf("非16进制字符,程序退出\n");
return;
}
}
buf[0] = temp[0]*16 + temp[1];
printf("buf[0] 为 0x%x\n",buf[0]);
}本回答被提问者采纳
void main(void)
{
unsigned char i,str[2],temp[2],buf[1];
str[0] = '6';
str[1] = '8';
for(i=0;i<2;i++)
{
if(str[i]>='0' && str[i]<='9') temp[i]=str[i]-'0';
else if(str[i]>='A' && str[i]<='F') temp[i]=str[i]-'A'+0x0A;
else if(str[i]>='a' && str[i]<='f') temp[i]=str[i]-'a'+0x0a;
else
{
printf("非16进制字符,程序退出\n");
return;
}
}
buf[0] = temp[0]*16 + temp[1];
printf("buf[0] 为 0x%x\n",buf[0]);
}本回答被提问者采纳