chriscalef Posted September 18, 2015 Share Posted September 18, 2015 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. :| Quote Link to comment Share on other sites More sharing options...
LukasPJ Posted September 18, 2015 Share Posted September 18, 2015 Not having the time to test the code atm, but have you tried it with just int's and floats? Quote Link to comment Share on other sites More sharing options...
Timmy Posted September 18, 2015 Share Posted September 18, 2015 Does it work using a memcpy instead of the strncpy? I think the T3D wrapper version is called dMemcpy (don't quote me on that lol). Quote Link to comment Share on other sites More sharing options...
chriscalef Posted September 18, 2015 Author Share Posted September 18, 2015 AAAAHHHHH....Thanks, Timmy! Yup, memcpy works fine. :-P Quote Link to comment Share on other sites More sharing options...
Timmy Posted September 19, 2015 Share Posted September 19, 2015 No probs, thought that would fix it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.