Search code examples
vectorbyteunsigned-integer

how to convert from vector<byte> to vector<uint_8> in c++?


In my C++ code , I have to use external library function which has the argument of vector<uint8_t>.

But in my source , I keep my variable as vector , so how would I do the conversation from vector<byte> to vector<unint8_t> while the variable gets passed into the function.

My source is given below.

//external library function from header:

uint16_t  GetLinkCRC16(const vector<uint8_t> &buffer);


vector<byte> m_Seq;

//calling in my cpp:

vector<byte> writeBytes(m_Seq);
GetLinkCRC16(writeBytes);

I have type conversation error when I pass "writeBytes" in to "GetLinkCRC16" method.


Solution

  • Unfortunately, there is "no way" to make the vector with differ type without making a copy (and type conversion).

    std::vector<byte> writeBytes(m_Seq);
    
    std::vector<uint8_t> writeBytesClone(writeBytes.begin(), writeBytes.end());
    GetLinkCRC16(writeBytesClone);
    

    What do I mean by saying "no way". There is a way, but it is a platform-dependent and, probably, UB way to wrap an array in a vector.