本文介绍了我怎样才能十六进制字符串赋值给一个char []变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我的十六进制字符串。 hexStr =AECF
我如何分配十六进制字符串值 unsigned char型myChar []
如下。谢谢你。
myChar [0] = 0xae;
myChar [1] = 0xcf;
解决方案
您可以将数据如下转换:
的char * hexstr =AECF;
INT hexsize = strlen的(hexstr);
无符号字符* myChar =新的无符号的char [hexsize / 2 + 1];
myChar [hexsize / 2] ='\\ 0'
对于(INT I = 0,INTJ = 0; I< hexsize;我+ = 2,J ++)
{
INT TMP;
sscanf的(hexstr + I,%2X,&安培; TMP));
myChar [J] = tmp目录; //警告,忽略它
}
这是如果你不使用静态字符串,否则使用其他的答案。
Assume my hex string. hexStr = "aecf"
How can I assign the hex string value to unsigned char myChar[]
as below. Thanks.
myChar[0] = 0xae;
myChar[1] = 0xcf;
解决方案
You may convert data as follows:
char* hexstr="aecf";
int hexsize=strlen(hexstr);
unsigned char* myChar = new unsigned char[hexsize/2+1];
myChar[hexsize/2]='\0'
for (int i=0,intj=0;i<hexsize;i+=2,j++)
{
int tmp;
sscanf(hexstr+i, "%2x",&tmp));
myChar[j]=tmp; // warning, ignore it
}
this is in case you do not use static strings, otherwise use other answers.
这篇关于我怎样才能十六进制字符串赋值给一个char []变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!