C++ Converting Vector<BYTE> to string where first vector byte is 0

爷,独闯天下 提交于 2020-07-08 00:22:50

问题


I am trying to convert a std::vector of BYTES (or unsigned char) to a std::string. The problem I have is when the first element of the vector is a 0, then this returns an empty string after the conversion.

I have tried the 2 methods below. Both string1 and string2 return an empty string. The result I am expecting is a string that starts with 2 x 0s followed by several other characters.

// vector of BYTE, contains these 7 elements for example: (0,30,85,160,155,93,0)
std::vector<BYTE> data;

// method 1
BYTE* pByteArray = &data[0];
std::string string1 = reinterpret_cast<LPCSTR>(pByteArray);

// method 2
std::string string2(data.begin(),data.end());

// both string1 and string2 return ""

I am guessing because the first BYTE in the vector is a 0, so the string assignment is thinking it's null or empty. Is there some other conversion I can do in order to return the rest of the string?

Any help greatly appreciated.


回答1:


The second is not really empty please consider:

// vector of BYTE, contains these 7 elements for example: (0,30,85,160,155,93,0)
std::vector<BYTE> data = {0, 35, 35 ,38};

// method 2
std::string string2(data.begin(),data.end());
cout<< (string2.data()+1) << " size:"<< string2.size() << endl;
/* Notice that size is 4 */

On ideone

Edit Checking the size is even more trivial as it's 4.


Regarding data and null termination as the docs kindly explain (emphasis mine):

The returned array is null-terminated, that is, data() and c_str() perform the same function. If empty() returns true, the pointer points to a single null character. (since C++11)

A "c++98 safe" way could look like:

cout.write(string2.data()+1, string2.size()-1); 

Anyway the printing is just to demonstrate the strings "non-emptiness" :)




回答2:


First method is wrong, however second gives correct result - string, that is null terminated. If termination mark (null) is the very first character, it looks empty, but actually it isn't.

Also, about first method, you wanted:

std::vector<BYTE> data;
//some code
std::string string1(reinterpret_cast<const char *>(&data[0]), data.length());



回答3:


If anyone is looking for hex output like 0x90 0xBA 0xFF then here's the code I've used to generate them:

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();
}


来源:https://stackoverflow.com/questions/25015126/c-converting-vectorbyte-to-string-where-first-vector-byte-is-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!