IntToHex(10, 2)
结果是字符串"OA"
下面两个函数是自己写的,
经常用到
String getStrFromHex(unsigned char *str, const int strSize, String tab)
{
char temp[2];
String s;
for (int i = 0;
i < strSize;
i ++)
{
sprintf(temp, "%02x", str);
if (i == 0)
s = s + temp;
else
s = s + tab + temp;
}
return s.UpperCase();
}
bool getHexFromStr(unsigned char *sendStr, int &size, String txtStr)
{
unsigned char char1, char2;
unsigned char *Senddata;
int len;
txtStr.Trim();
len = txtStr.Length();
if (len == 0)
{
MessageBox(NULL,"情输入内容!","出错信息",MB_OK);
return false;
}
Senddata = txtStr.c_str();
size = len / 2;
int i = 0;
for (;
i < size;
i ++)
{
char1 = Senddata[2*i];
char2 = Senddata[2*i + 1];
if(char1>='0'&&
char1<='9')
char1=(char1-'0')*16;
else
if(char1>='a'&&
char1<='f')
char1=((char1-'a')+0x0A)<<4;
else
if(char1>='A'&&
char1<='F')
char1=((char1-'A')+0x0A)<<4;
else
{
MessageBox(NULL,"含非有效数字!","出错信息",MB_OK);
return false;
}
if(char2>='0'&&
char2<='9')
char2=char2-'0';
else
if(char2>='a'&&
char2<='f')
char2=(char2-'a')+0x0A;
else
if(char2>='A'&&
char2<='F')
char2=(char2-'A')+0x0A;
else
{
MessageBox(NULL,"含非有效数字!","出错信息",MB_OK);
return false;
}
sendStr=char1+char2;
}
return true;
}