简体   繁体   English

C++ 无法转换 std::vector<byte> 到字符串或字符数组</byte>

[英]C++ Unable to convert std::vector<BYTE> to a string or char array

I am currently working with the Registry using this GitHub library:我目前正在使用这个 GitHub 库处理注册表:

https://github.com/GiovanniDicanio/WinReg https://github.com/GiovanniDicanio/WinReg

I am trying to convert this vector<BYTE> to a char array or a string, to make a hash out of it with help of SHA-512.我正在尝试将此vector<BYTE>转换为 char 数组或字符串,以便在 SHA-512 的帮助下从中生成 hash。 But I am stuck with converting it, I tried different methods.但我坚持转换它,我尝试了不同的方法。 I don´t get any compiler errors, just the app crashes at runtime.我没有收到任何编译器错误,只是应用程序在运行时崩溃了。 I am using a DLL that I load into my process.我正在使用加载到我的进程中的 DLL。

 RegKey NetworkInterface_key(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}\\0001");

 const std::vector<BYTE> InstallTimeStamp = NetworkInterface_key.GetBinaryValue(L"InstallTimeStamp");

MY SOLUTION:我的解决方案:

Changed std::vector<BYTE> -> std::vector<unsigned char>更改了std::vector<BYTE> -> std::vector<unsigned char>

Used this methode:使用此方法:

template <typename T>
std::string to_hex(T data)
{
    std::ostringstream result;
    result << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << static_cast<int>(data);
    return result.str();
}

std::string dump(const std::vector<unsigned char>& data)
{
    if (data.empty()) return "";
    auto size = data.size();
    std::ostringstream result;
    for(u32 i =0; i < size; i++)
    {
        result << "0x" + to_hex(data[i]);
        if (i != size)
            result << " ";
    }
    return result.str();
}

Credits: U. Bulle -> C++ Converting Vector<BYTE> to string where first vector byte is 0致谢:U. Bulle -> C++ Converting Vector<BYTE> to string where first vector byte is 0

You don't need that library, just do this:您不需要那个库,只需执行以下操作:

HKEY key = 0;
BYTE timestamp[16] = { 0 };
LRESULT err = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}\\0001", 0, KEY_READ, &key);
if (err == 0)
{
    DWORD dwType = 0;
    DWORD dwSize = 16;
    ::RegQueryValueEx(key, L"InstallTimeStamp", NULL, &dwType, timestamp, &dwSize);
    RegCloseKey(key);
}

As for converting those 16 bytes into "string".至于将这 16 个字节转换为“字符串”。 That doesn't make a lot of sense given that that those 16 bytes are binary data.鉴于这 16 个字节是二进制数据,这没有多大意义。 You could do this:你可以这样做:

std::string strTimestamp((char*)timestamp, 16);

But I suspect you just want a pointer to pass to a sha512 function that expects a char* data type.但我怀疑您只是想要一个指针传递给需要char*数据类型的 sha512 function 。 If that's the case, just do this:如果是这种情况,只需执行以下操作:

const char* ts = (char*)timestamp;

Just remember the length of that array is fixed and is not a null terminated string.请记住,该数组的长度是固定的,不是以 null 结尾的字符串。 So your hash function should take a length parameter as well.所以你的 hash function 也应该有一个长度参数。

The RegKey::GetBinaryValue() method returns a std::vector<BYTE> . RegKey::GetBinaryValue()方法返回一个std::vector<BYTE> To convert that data to a char[] array, you don't really have to actually convert it at all, you can simply type-cast a pointer to the data instead:要将该数据转换为char[]数组,您实际上根本不需要实际转换它,您可以简单地类型转换一个指向数据的指针:

const std::vector<BYTE> InstallTimeStamp = ...;
const char *pInstallTimeStamp = reinterpret_cast<const char*>(InstallTimeStamp.data());

But, if you want to convert the data to a std::string , then std::string has constructors that are appropriate for that purpose, eg:但是,如果您想将数据转换为std::string ,则std::string具有适合该目的的构造函数,例如:

const std::vector<BYTE> InstallTimeStamp = ...;
std::string sInstallTimeStamp(reinterpret_cast<const char*>(InstallTimeStamp.data()), InstallTimeStamp.size());
const std::vector<BYTE> InstallTimeStamp = ...;
std::string sInstallTimeStamp(InstallTimeStamp.begin(), InstallTimeStamp.end());

However, that being said, hashes operate on bytes, not on characters or strings, so you really should not need to convert the vector data to anything else at all, just hash its contents as-is.然而,话虽这么说,散列是对字节进行操作,而不是对字符或字符串进行操作,因此您实际上根本不需要将vector数据转换为其他任何内容,只需 hash 其内容原样即可。 Unless you are using a hashing API that requires char / string input (if so, you should find a better hash API), in which case the above should suffice.除非您使用需要char / string输入的散列 API(如果是这样,您应该找到更好的 hash API),在这种情况下,上述内容就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM