BYTE数组与16进制字符串互转

//字节数组转换为HEX 字符串
const string Byte2HexString(const unsigned char* input, const int datasize)
{
char output[datasize*];
for(int j = ; j < datasize; j++ )
{
unsigned char b = *(input+j);
snprintf( output+j * ,, "%02x",b);
}
return string(output) ;
}
/*
* 把16进制字符串转换成字节数组 @param hex @return
*/
unsigned char* HexString2Byte(string hex)
{
int len = (hex.length() / );
unsigned char* result = new unsigned char[len];
//const char* achar = hex.c_str();
for (int i = ; i < len; i++) {
int pos = i * ; QString lStr(hex[pos]);
int iLeft = lStr.toInt(NULL, ); QString rStr(hex[pos+]);
int iRight = rStr.toInt(NULL, ); int iNew = ( iLeft<< | iRight);
result[i] = (unsigned char) iNew;
}
return result;
}

调用演示:

BYTE face_feature[];

//16进制字符串转换为字节数组
unsigned char* pFeature = HexString2Byte(strFeature);
memcpy(face_feature, pFeature, strFeature.length()/); //字节数组转换为16进制字符串
string strFeature = Byte2HexString(face_feature,);
上一篇:自动化测试框架的Step By Step搭建及测试实战(1)


下一篇:HDU 4729 An Easy Problem for Elfness (主席树,树上第K大)