tags:

views:

95

answers:

2

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!

+4  A: 

First of all, never send ints and other types larger than char like that - you will ignore endianness and the recipient might be unable to interpret the data properly.

Second, you can overload the method to accept built-in types and use functions like htons() inside for converting the data to network layer order in machine-independent fashion.

If you still want to use vector - use something like this:

vector<char> temp( sizeof( yourVariable ) );
memcpy( &temp[0], &yourVariable, sizeof( yourVariable ) );
sharptooth
I ommited the functions that handle endian issues in my example. Internally all multibyte types are correctly converted to network byte order.
nabulke
@nabulke: Okay, then here you go.
sharptooth
+4  A: 

Ignoring endianess:

template< typename T >
char* begin_binary(const T& obj) {return reinterpret_cast<char*>(&obj);}
template< typename T >
char* end_binary  (const T& obj) {return begin_binary(obj)+sizeof(obj);}

int num = 0x01234567;
vector<char> whatIWant( begin_binary(num), end_binary(num) );

However, I would use unsigned char as a byte type.

I feel the need to add to this that, as always, the use of reinterpret_cast makes the result of this implementation-specific. I think one could imagine (though barely) an implementation where char has stricter alignment than some type used for T and that reinterpret_cast would 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.

sbi
Wow. How to drive a screw into a wooden board using iterators...
sharptooth
@sharptooth: Um, I'm not sure whether this is approving or disapproving...
sbi
@sbi: That's neutral. Surely it will work and it is "the iterators way" and it writes the vector elements once (while in my answer they are written twice), but it makes me once again wonder if there's anything that can't be done with iterators.
sharptooth
@sharptooth: It's not done for iterator's sake, but for `std::vector`'s. The only `std::vector` ctor accepting existing data takes iterators, and in order to use that, you need to pass iterators.
sbi
@sbi: Yes, I understand the intent behind that. Writing data into the buffer without zeroing out the buffer first is clearly better.
sharptooth