c++两个常用函数:1.二进制字符串2 hex(十六进制)。2. 栈变量字符串2堆变量

二进制字符串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());
}

 

栈变量字符串2堆变量

const char* CopyStr(const std::string& strResult)
{
 //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;

}

你可能感兴趣的:(c++)