c++字符串转16进制
std::string fromStringToHex( std::string _szData )
{
char *pcTemp = (char *)calloc(1UL, _szData.size() * 3 + 1);
int iCurPos = 0;
if (NULL != pcTemp)
{
for (int i = 0; i < _szData.size(); ++i)
{
iCurPos += sprintf(pcTemp + iCurPos, "%02X ", (unsigned char)_szData[i]);
}
pcTemp[iCurPos] = 0;
}
std::string szRet = std::string(pcTemp);
free(pcTemp);
retrun szRet;
}
这段代码演示了如何使用C++将一个字符串转换成16进制表示。通过遍历输入字符串的每个字符,并使用`sprintf`将其转换为格式化的16进制字符串,然后拼接到结果中。
1万+

被折叠的 条评论
为什么被折叠?



