我有一个 MD5 字符串,我正在将其转换为十六进制。有一个更好的方法吗?我目前正在做:
unsigned char digest[16];
string result;
char buf[32];
for (int i=0; i<16; i++)
{
sprintf_s(buf, "%02x", digest[i]);
result.append( buf );
}
Run Code Online (Sandbox Code Playgroud)
这个版本应该更快。如果您需要更高的速度,请更改string result为char数组。
static const char hexchars[] = "0123456789abcdef";
unsigned char digest[16];
string result;
for (int i = 0; i < 16; i++)
{
unsigned char b = digest[i];
char hex[3];
hex[0] = hexchars[b >> 4];
hex[1] = hexchars[b & 0xF];
hex[2] = 0;
result.append(hex);
}
Run Code Online (Sandbox Code Playgroud)