简体   繁体   中英

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

I am currently working with the Registry using this GitHub library:

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. 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.

 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>

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

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". That doesn't make a lot of sense given that that those 16 bytes are binary data. 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. 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. So your hash function should take a length parameter as well.

The RegKey::GetBinaryValue() method returns a 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:

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:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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