0

我正在尝试找到一种非常轻量级的方法来对 char 或 md5 进行十六进制处理,下面有两个示例。

char example[0x34];
char example_final[0x200];
sprintf(example, "%s", "4E964DCA5996E48827F62A4B7CCDF045");

下面以使用两个 PHP 函数为例。

MD5:

sprintf(example_final, "%s", md5(example));
output: 149999b5b26e1fdeeb059dbe0b36c3cb

十六进制:

sprintf(example_final, "%s", bin2hex(example));
output: 3445393634444341353939364534383832374636324134423743434446303435

这是最轻量级的方法。我在想一些大的字符数组到它的十六进制值而不是循环foreach char,但不确定。

4

1 回答 1

5

的示例bin2hex如下所示:

#include <string>
#include <iostream>


std::string bin2hex(const std::string& input)
{
    std::string res;
    const char hex[] = "0123456789ABCDEF";
    for(auto sc : input)
    {
        unsigned char c = static_cast<unsigned char>(sc);
        res += hex[c >> 4];
        res += hex[c & 0xf];
    }

    return res;
}


int main()
{
    std::string example = "A string";

    std::cout << "bin2hex of " << example << " gives " << bin2hex(example) << std::endl;
}
于 2013-08-24T23:48:59.297 回答