F
Fei Liu
hurcan said:Hi all, I am trying to convert a float value to a octet stream for
transmission, I came up with solution like
float deneme=3.14156789;
float deneme2=0.0;
vector<unsigned char> vec; //this is my buffer can con contain
other things besides this float
int* val=reinterpret_cast<int*>(&deneme); // gives me jeebies
vec.push_back((unsigned char) ((*val >> 24) & 0xFF ));
vec.push_back((unsigned char) ((*val >> 16) & 0xFF ));
vec.push_back((unsigned char) ((*val >> 8) & 0xFF ));
vec.push_back((unsigned char) ((*val >> 0) & 0xFF ));
int val2=0;
int _pos=0;
val2+= ((int)vec[_pos++] & 0xFF) << 24;
val2+= ((int)vec[_pos++] & 0xFF) << 16;
val2+= ((int)vec[_pos++] & 0xFF) << 8;
val2+= ((int)vec[_pos++] & 0xFF) << 0;
deneme2=*reinterpret_cast<float*>(&val2); //ditto
it works, but i am not sure that this is the Right Way to do, I
wonder if there is
a safer or cleaner way? I tried memcpy to treat the vector as if
an array it didn't
work out well( although, It should work right?)
Any pointers would be greatly appreciated...
hurcan
Did you look into more general framework such as soap or yaml? Boost
also has a primitive serialization/deserialization framework.
Fei