My TcpClient class accepts vector<char> in its SendData method like this:
void CTcpClient::SendData(const vector<char>& dataToTransmit)
Therefore, in order to use the function, I have to convert any built in type (int, long, short, long long) to a vector<char>.
I tried several solutions using streams but always end up with an ASCII representation of the number I want to convert (I also tried to use binary flags without success). But I need the binary values of the numbers.
For example:
int num = 0x01234567
vector<char> whatIWant = {0x01, 0x23, 0x45, 0x67}
What solution would you suggest?
Thanks for any help!
Ignoring endianess:
However, I would use
unsigned charas a byte type.I feel the need to add to this that, as always, the use of
reinterpret_castmakes the result of this implementation-specific. I think one could imagine (though barely) an implementation wherecharhas stricter alignment than some type used forTand thatreinterpret_castwould trigger a hardware exception. However, I consider this possibility rather academic.Further, the two functions would probably benefit from a compile-time assertion restricting
T. Commonly, pointers,struct‘s containing pointers, and non-POD types shouldn’t be used with this.