二进制字符串2 hex
string get_b2hex(const char * source,int len)
{
std::string strHexPack;
for (int i = 0; i < len; ++i)
{
unsigned char c = source[i];
unsigned int nIntVal = c;
char hex_buf[10] = {0};
sprintf(hex_buf, "%02x", nIntVal);
strHexPack += hex_buf;
}
return strHexPack;
//printf("*********strHexPack:%s/n", strHexPack.c_str());
}
{
std::string strHexPack;
for (int i = 0; i < len; ++i)
{
unsigned char c = source[i];
unsigned int nIntVal = c;
char hex_buf[10] = {0};
sprintf(hex_buf, "%02x", nIntVal);
strHexPack += hex_buf;
}
return strHexPack;
//printf("*********strHexPack:%s/n", strHexPack.c_str());
}
栈变量字符串2堆变量
const char* CopyStr(const std::string& strResult)
{
//const char* '/0'
const char * t_t_tempchar = (const char *)strResult.data();
{
//const char* '/0'
const char * t_t_tempchar = (const char *)strResult.data();
char * charResult = (char*)malloc(sizeof(char) * strResult.size() + 1);
charResult[strResult.size()] = '/0';
strcpy(charResult, t_t_tempchar);
return charResult;
}
本文转自elbertchen 51CTO博客,原文链接:http://blog.51cto.com/linkyou/750675,如需转载请自行联系原作者