Integral types can be casted one to another with not many problems.
Just notice that if the unsigned number has a high value, you will get a negative number in the signed version of the same type
...
You're directly casting an integer to a buffer that will be sent over a network? That's one of the worst things you could possibly do.
You're supposed to first manually convert it to avoid endianness problems:
1 2 3 4 5
char buffer[]={
unsigned(n)&0xFF, //when twiddling bits, it's usually better to use unsigned types
unsigned(n)>>8
}
SendData(buffer,2);
Then when you receive the data on the other end, you perform the inverse operation.
No no, I'm not casting before sending data over the network.
The Network library I use retreives data like uint8_t* while I have sent a buffer of 'signed short*'.
For now I'm using the direct casting and it works:
1 2 3 4 5 6 7 8 9
typedefsignedshort data_type;
DataArrived(uint8_t* data, size_t size)
{
data_type* mybuffer = (data_type*)data;
size /= sizeof(data_type);
.... save mybuffer into vector<data_type>
}