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.141567 89;
float deneme2=0.0;
vector<unsigned charvec; //this is my buffer can con contain
other things besides this float
int* val=reinterpret _cast<int*>(&de neme); // 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=*reinte rpret_cast<floa t*>(&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
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
transmission, I came up with solution like
float deneme=3.141567 89;
float deneme2=0.0;
vector<unsigned charvec; //this is my buffer can con contain
other things besides this float
int* val=reinterpret _cast<int*>(&de neme); // 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=*reinte rpret_cast<floa t*>(&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
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Comment