Code: Select all
unsigned int size = str.size();
unsigned char* sref = (unsigned char*)&size;
unsigned int offset = (unsigned int)sref + 4;
for (; (unsigned int)sref < offset; ++sref)
{
packet.push_back(*sref);
}
Code: Select all
unsigned int size = str.size();
unsigned char* sref = (unsigned char*)&size;
unsigned int offset = (unsigned int)sref + 4;
for (; (unsigned int)sref < offset; ++sref)
{
packet.push_back(*sref);
}
Code: Select all
#include <array>
#include <vector>
#include <string>
#include <algorithm>
#include <string_view>
template <class T> void x(std::vector<uint8_t> & packet, const T & stlc) {
using U = decltype(stlc.size());
U n = stlc.size();
uint8_t *p = reinterpret_cast<uint8_t*>(&n);
uint8_t *o = p+sizeof(U);
for (; p < o; ++p) {
packet.push_back(*p);
}
}
void y(std::vector<uint8_t> & packet) {
x(packet, std::string("0123456789abcdefghijk"));
}
Code: Select all
template <class T> void x(std::vector<uint8_t> & packet, const T & stlc) {
std::copy_n(std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t*>(std::array<decltype(stlc.size()),1>{stlc.size()}.data()), sizeof(decltype(stlc.size()))).begin(), sizeof(decltype(stlc.size())), std::back_inserter(packet));
}