69,381
社区成员
发帖
与我相关
我的任务
分享
char *num = a;//得到字符串 "135" Num 21 C-Octet String :
unsingned int mType = (unsigned int)a[21];//因为 Num 占了前面21个位置 , type 1 Unsigned Integer
char *codeA= a+22;// 前面已经用了 21+1 个字节 , 4 C-Octet String
char *codeB= a+26;// 前面已经用了 21+1+4个字节 4 C-Octet String
char *times= a+30;// 前面已经用了 21+1+4+4个字节. 2 Octet String
// 注意,你必须优先校验 输入字符串 的准确性(至少长度要正确),
// 否则,就会出现我上面提到的第二点:times明明是2个字节,而且是字符串(意味着最后一个是0),但输入的字符串却只剩下1个字节,这么以来, times 这个变量就没有结束字符,一旦调用,就会出错.
char _toHexChar(char ch_hexstr)
{
if(ch_hexstr>='0' && ch_hexstr<='9')
{
return ch_hexstr-'0';
}
if (ch_hexstr>='a' && ch_hexstr<='f')
{
return ch_hexstr-'a'+10;
}
if (ch_hexstr>='A' && ch_hexstr<='F')
{
return ch_hexstr-'A'+10;
}
if (ch_hexstr==' ' || ch_hexstr==',')
{
return -1;
}
return -2;
}
//通用 字符串-->十六进制数组 转换模板
template<typename _chartype,typename _hextype>// _chartype=char/wchar_t _hextype=uchar/ushort/uint
long __template_hexstr_to_array(const _chartype *s,_hextype *ret_hex,int hexBuffLen)
{
static int iSize = sizeof(_hextype)*2;
if (!s || !ret_hex)//输入或输出为空
{
return 0;
}
int i=0,//str index
j=0,//bit index
ss=0;//sum
_hextype hextmp;
//char bit4[sizeof(_hextype)];// int64需要16个字节缓冲区
char ctype;
while(s[i])
{
//丢弃 0x 和 0X
if (s[i]=='0' && (s[i+1]=='x'|| s[i+1]=='X'))
{
i+=2;
if (s[i]=='0' && (s[i+1]=='x'|| s[i+1]=='X'))
{
break;//连续出现2个 0x
}
continue;
}
//分组,同时转换
hextmp = 0;
ctype = 0;
for(j=0;j<iSize && ctype>=0;j++)
{
if((ctype = _toHexChar((char)s[i])) >=0 )
{
hextmp = (hextmp<<4) | ctype;
i++;
}else
{
i++;
continue;//exit for
}
}
if (j==1)//第一个就是无效字符了,丢弃这个分组
{
continue;
}
ret_hex[ss] = hextmp;
ss++;
if (hexBuffLen && ss>=hexBuffLen)//输出缓冲区长度不够
{
break;
}
}
return ss;
};
//使用:
char *str = "1234545776746345353";
char ret_hex[100];//输出缓冲区,保证缓冲区足够大
long retsum = __template_hexstr_to_array<char,char>(str,ret_hex,100);
//retsum 是转换后的十六进制的长度.