0

For some project i need to send encoded messages but i can only give vetor of uint8_t to be sent, and i have a char array (with numbers and string i converted to hexadecimal in it) and a pointer on the array. I encode the msg which is an object into the buffer then i have to send it and decode it etc.

char buffer[1024]
char *p = buffer
size_t bufferSize = sizeof(buffer)
Encode(msg,p,bufferSize)

std::vector<uint8_t> encodedmsg; //here i need to put my message in the buffer
Send(encodedmsg.data(),encodedmsg.size()) //Only taking uint8_t vector

Here is the prototype of send :

uint32_t Send(const uint8_t * buffer, const std::size_t bufferSize)                                                                         

I already looked at some questions but no one have to replace it in a vector or convert to uint8_t. I thinked bout memcpy or reinterpreted cast or maybe using a for loop but i don't really know how to do it whitout any loss.

Thanks,

1 Answer 1

4

Actually your code suggest that Send() function takes pointer to uint8_t, not std::vector<uint8_t>. And since char and uint8_t has same memory size you just could do:

Send(reinterpret_cast<uint8_t*>(p), bufferSize);

But if you want to do everything "right" you could do this:

encodedmsg.resize(bufferSize);
std::transform(p, p + bufferSize, encodedmsg.begin(), [](char v) {return static_cast<uint8_t>(v);});
Sign up to request clarification or add additional context in comments.

10 Comments

thanks a lot i'll try, actually i did " std::vector<uint8_t> encodedmsg(buffer, buffer + bufferSize / sizeof(char)); " but clearly not working.
This way i can use v.data() and v.size() in my send function?
Interesting... std::vector<uint8_t> encodedmsg(buffer, buffer + bufferSize / sizeof(char)); should also work. Are you sure you need to send entire bufferSize data, not some other value reurned by Encode() for example? And what does it mean exactly by "not workig"?
Yes, of course you can use v.data() and v.size() anywhere when you have ready vector.
First when using your resize transform way i got 3 errors: error: expected primary-expression before '[' token error: expected primary-expression before ']' token expected primary-expression before 'char'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.