C++:如何将带有 md5 哈希的 wstring 转换为字节* 数组
C++: How to convert wstring with md5 hash to byte* array?
std::wstring hashStr(L"4727b105cf792b2d8ad20424ed83658c");
//....
byte digest[16];
如何在摘要中获取我的 md5 哈希?我的回答是:
wchar_t * EndPtr;
for (int i = 0; i < 16; i++) {
std::wstring bt = hashStr.substr(i*2, 2);
digest[i] = static_cast<BYTE>(wcstoul(bt.c_str(), &EndPtr, 16));
}
你需要
从hashStr
中读取两个字符,将它们从十六进制转换为二进制值,并将该值放入digest
的下一个位置 - 按此顺序:
for (int i=0; i<16; i++) {
std::wstring byte = hashStr.substr(i*2, 2);
digest[i] = hextobin(byte);
}
C-way(我没有测试它,但它应该可以工作(尽管我可以在某个地方搞砸了(,无论如何你都会得到该方法(。
memset(digest, 0, sizeof(digest));
for (int i = 0; i < 32; i++)
{
wchar_t numwc = hashStr[i];
BYTE numbt;
if (numwc >= L'0' && numwc <= L'9') //I assume that the string is right (i.e.: no SGJSGH chars and stuff) and is in uppercase (you can change that though)
{
numbt = (BYTE)(numwc - L'0');
}
else
{
numbt = 0xA + (BYTE)(numwc - L'A');
}
digest[i/2] += numbt*(2<<(4*((i+1)%2)));
}
相关文章:
- 将日语 wstring 转换为 std::string
- 从 uint32_t 转换为 wchar_t 并存储在 wstring 中时出现访问冲突
- 如何将std::wstring转换为char const[]
- 将utf16宽std::wstring转换为utf8窄std::string以获得罕见字符时出现问题
- 将 char* argv[] 转换为 wstring
- 将 std::string 转换为具有特殊字符的 FString (TCHAR / wstring)
- 在 C++ 中将 utf8 wstring 转换为窗口中的字符串
- 如何将 wstring 转换为 wchar_t*?C++
- 如何从 LPWSTR** 转换为 std::vector<std::wstring>
- 从 std::wstring 转换为 jstring
- 将 std::u16string 转换为 std::wstring,无需复制
- 如何将WSTRING中的Unix时间戳转换为char数组中格式的日期
- 转换一个带有UTF8 WSTRING控制台的装饰的字符串
- 如何将平台^字符串转换为wstring,然后与L字符串连接
- 在窗口上将格式化的 C 字符串和参数转换为 wstring
- 为什么我的 BSTR 到 std::wstring 的转换这么慢?我的测试仪不好吗?
- 在std::string和std::wstring之间转换的多平台方式
- 正在将std::wstring转换为const wchar_t x[]
- 使用字符串构造函数从wstring转换为字符串时,是否没有可能的数据丢失
- 将 std::wstring 转换为 SQLWCHAR *