What would be the correct way to convert from unsigned char std::vector to char?
void SendSocket(const std::vector<BYTE> &buffer)
{
int ret;
const BYTE* bufferPtr = &buffer[0];
ret = send(_socket, (const char*)bufferPtr, buffer.size(), 0);
}
Assuming that
BYTEis a typedef forchar(with or withoutsignedorunsigned), then your code is fine, but slightly more verbose than necessary. The storage used by avectoris required to be a contiguous array, so taking the address of the first element gives you a pointer to that array. Any kind ofcharhas the same layout and alignment as any other kind ofchar, so the pointer conversion is valid.The argument to
sendisconst void*, and (more or less) any pointer can be converted to that implicitly, so there’s no need to cast:However, you should check (or otherwise ensure) that
bufferisn’t empty before using[].