user14895313
user14895313

Reputation:

Append different types of data to a vector of bytes

I have a vector of byte which I wanted to store different types of data to it. I have defined that vector as packed_data which you can see it in the following member function of my class.

Now I wanted to get different type of data with the PackedData member function and then append those information to the raw_data vector as byte.

// Here I initialize a single vector with the vectors value

std::vector<BYTE> PackedData()
{
    std::vector<BYTE> packed_data;

    // Here I have initialized the header with two byte
    packed_data.insert(packed_data.end(), m_header.begin(), m_header.end());

    // I specified encoding of the packet
    packed_data.insert(packed_data.end(), m_messageEncoding);

    // I specified message type
    packed_data.insert(packed_data.end(), m_messageType);

    // I specified size of payload should send to the server
    packed_data.insert(packed_data.end(), m_messageSize);

    // I specified payload itself must send to the server
    packed_data.insert(packed_data.end(), m_payload.begin(), m_payload.end());

    // I specified footer 
    packed_data.insert(packed_data.end(), m_footer.begin(), m_footer.end());

    return packed_data;
}

I haven't any problem to add vector to the packed_data but When I wanted to store size of my message (it has unsigned int data type) to the vector of bytes as a 4 byte entity, I couldn't get correct data from the vector.

For example, when I pass 512 (decimal value) as an input for m_messageSize (unsigned int), it just append one byte to the packed_data vector (0x20 for example).

In the following section, also you can see constructor of my class which I am going to initialize my member fields there:

CRawData(MessageType arg_message_type, MessageEncoding arg_encoding, unsigned int arg_total_size, std::vector<BYTE> arg_payload)
{
    // Header Initialization
    m_header.push_back(0xAA);
    m_header.push_back(0xAA);
    
    // Send / Request / Verification Specifier
    m_messageType = arg_message_type;
    
    // Encoding Specifier
    m_messageEncoding = arg_encoding;
    
    // Number of chunks should sent
    m_messageSize = arg_total_size;
    
    // Payload initialization
    m_payload = arg_payload;
    
    // Footer specifier
    m_footer.push_back(0xFF);
    

m_footer.push_back(0xFF);
}

Indeed I wanted to append something like 00 00 20 00 to my vector. How can I manage this goal? If more information you need, please request in comment which I can edit my question to the right way.

Upvotes: 1

Views: 1994

Answers (2)

Caleth
Caleth

Reputation: 63162

If BYTE is an alias for char, signed char, unsigned char or std::byte, you can reinterpret_cast pointers to objects to pointers to BYTE, and use the InputIt overload of vector::insert.

This is only sensible if all your data are of trivial types.

std::vector<BYTE> PackedData()
{
    std::vector<BYTE> packed_data;

    // Here I have initialized the header with two byte
    packed_data.insert(packed_data.end(), m_header.begin(), m_header.end());

    // I specified encoding of the packet
    packed_data.insert(packed_data.end(), reinterpret_cast<BYTE*>(&m_messageEncoding), reinterpret_cast<BYTE*>(&m_messageEncoding + 1));

    // I specified message type
    packed_data.insert(packed_data.end(), reinterpret_cast<BYTE*>(&m_messageType), reinterpret_cast<BYTE*>(&m_messageType + 1));

    // I specified size of payload should send to the server
    packed_data.insert(packed_data.end(), reinterpret_cast<BYTE*>(&m_messageSize), reinterpret_cast<BYTE*>(&m_messageSize + 1));

    // I specified payload itself must send to the server
    packed_data.insert(packed_data.end(), m_payload.begin(), m_payload.end());

    // I specified footer 
    packed_data.insert(packed_data.end(), m_footer.begin(), m_footer.end());

    return packed_data;
}

Upvotes: 0

jrok
jrok

Reputation: 55425

You need to treat an unsinged int as an array of bytes, too, otherwise it gets converted to a single byte and the rest of data is lost. So something like:

BYTE* msgType_begin = reinterpret_cast<BYTE*>(&m_messageType);
packed_data.insert(packed_data.end(), msgType_begin, msgType_begin + sizeof(m_messageType));

Upvotes: 0

Related Questions