在 win32 c++ 中以十六进制 returns 0x7FFFFFFF 打印出 DWORD

Printing out DWORD in hex returns 0x7FFFFFFF in win32 c++

我目前正在开发 win32 应用程序,该应用程序在字符串上使用 sha1 哈希来为我的地图生成键。我想将散列用作 DWORD,因此我可以计算散列之间的一些计算。

由于 sha1 哈希重现 160 位哈希值,我创建了一个包含 5 DWORD(32 位 * 5 = 160)的结构。

struct _SHA1_HASH
{
   DWORD dwFirst;
   DWORD dwSecond;
   DWORD dwThird;
   DWORD dwFourth;
   DWORD dwFifth;
}SHA1_HASH;

我已经对字符串进行哈希处理并存储到结构中。

void GenerateNodeKey()
{
    // SHA-1 Hashing Process
    // ex) strHash = 356A192B7913B04C54574D18C28D46E6395428AB    

    SHA1_HASH key;
    string s_prefix = "0x";
    key.dwFirst = strtol((s_prefix+strHash.substr(0,8)).c_str(), 0, 0); // first 8 bytes
    ...
    ...
    key.dwFifth = strtol((s_prefix+strHash.substr(32,8)).c_str(), 0, 0); // last 8 bytes
}

但是当我尝试打印值以检查它们是否正常时出现了问题。

    wcout << hex 
          << "Node ID : " << key.dwFirst << key.dwSecond << key.dwThird << key.dwFourth << key.dwFifth 
          << dec
          << endl;

如果 8 字节的十六进制值小于 7FFFFFFF (2,147,483,647),则打印没有问题。但是,当它大于该值时,它只打印出最大数量。我在网上查了下DWORD能装多大,没问题(相关:What is the largest integer number base 16 that can be store in a variable of type dword?

有人可以帮忙吗? 提前致谢。

strtol 函数 return 是一个带符号的长整数,在 windows 上的范围是 -2,147,483,648 to 2,147,483,647

如果strtol检测到生成的数字超出范围,则return最大long值。来自 cppreference:

If the converted value falls out of range of corresponding return type, a range error occurs (setting errno to ERANGE) and LONG_MAX, LONG_MIN, LLONG_MAX or LLONG_MIN is returned.

改用std::strtoul