Jump to content

inexplicable problem re: reinterpret_cast


chriscalef

Recommended Posts

Pardon me, but do you have a moment to talk about reinterpret_cast...?


I am currently working on a networking layer item that requires converting a set of values of various data types into an array of bytes (char *), and then back into int, float, double etc. values. I've been doing this for some time through a shortcut of converting all numeric values into floats, and simply ignoring strings because I didn't need any at the moment. (Sorry, I was in a hurry.) But recently I revisited this code and decided to make an earnest attempt at generalizing it.


However, right after I worked out a nice systematic way to transfer a series of bytes through a socket and break it back out into whatever data type it came from, for some odd reason my float values started coming up zero every time. Ints and shorts and doubles are working fine, but when I run the following code, my floatValue reports zero, every time.


It's doubly weird because up until now I've been using nothing _but_ floats for everything... but now that I started introducing short, int, and double, everything works great _except_ floats. #fml

 

	
char bytes[8];
char *buf;
buf = new char[1024];

short shortValue = 25;
int intValue = 36793;
float floatValue = 4.5;
double doubleValue = 7.89;

int byteCounter = 0;
strncpy(&buf[byteCounter], reinterpret_cast<char*>(&shortValue),sizeof(short));
byteCounter += sizeof(short);	
strncpy(&buf[byteCounter], reinterpret_cast<char*>(&intValue),sizeof(int));
byteCounter += sizeof(int);
strncpy(&buf[byteCounter], reinterpret_cast<char*>(&floatValue),sizeof(float));
byteCounter += sizeof(float);	
strncpy(&buf[byteCounter], reinterpret_cast<char*>(&doubleValue),sizeof(double));


byteCounter = 0;
for (int i=0;i<sizeof(short);i++) bytes[i] = buf[byteCounter+i];
short *shortPtr = reinterpret_cast<short*>(bytes);
shortValue = *shortPtr;	
byteCounter += sizeof(short);

for (int i=0;i<sizeof(int);i++) bytes[i] = buf[byteCounter+i];
int *intPtr = reinterpret_cast<int*>(bytes);
intValue = *intPtr;
byteCounter += sizeof(int);

for (int i=0;i<sizeof(float);i++) bytes[i] = buf[byteCounter+i];
float *floatPtr = reinterpret_cast<float*>(bytes);
floatValue = *floatPtr;
byteCounter += sizeof(float);

for (int i=0;i<sizeof(double);i++) bytes[i] = buf[byteCounter+i];
double *doublePtr = reinterpret_cast<double*>(bytes);
doubleValue = *doublePtr;
byteCounter += sizeof(double);

Con::printf("shortValue %d intValue %d floatValue %f doubleValue %g byteCounter %d",
				shortValue,intValue,floatValue,doubleValue,byteCounter);

 

If anyone has a moment to cut and paste the code and report the results, I'd be most appreciative. :|

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...