TReal from TBuf8 buffer
| Sat, 2008-06-14 05:38 | |
|
I have what may be a stupid question... but I seem to be having a brain fart. What I have is a TBuf8 that contains a set of bytes that had been loaded from an SMS message. The bytes in the buffer are stored in reverse order, so I use the LittleEndian static class to read values from the buffer and correct the byte order. So for example, I read a TUint from the buffer using the LittleEndian::Get32() function. This works just fine. The problem is that I have a 32-bit value in the buffer that is/was supposed to be a TReal32 value. It is also in reverse order (or so it appears)... So in the middle of the buffer is a 32-bit sequence like the following (41A7ED1B). I need to reverse this (1BEDA741) and put the data into a TReal32 type while retaining the floating point interpretation. The Get32() function of the LittleEndian class assumes that this is an TUint so I cant use it in this case. Does anybody know how I can get this into the TReal32 variable? I can't use TLex as this is for taking floats that are text based and converting them to TReal... i.e. 11.32 can be put in to a TReal32 using the TLex8 class. Unfortunately I don't have access to the software that creates the data that is pulled in from the SMS message... so I have to live with the data as is and figure out how to parse it into the correct values. Any advice? Thanks, GSM |
|






Forum posts: 1242
I don't know whether there is any elegant way to do this, but in C/C++ you can always do it the hard way: The "union" construct allows you to easily interpret a number of bytes in different ways.
In your case, I would define a union with an array of 4 TUint8/unsigned char and a TReal32. I would then fill this little 4-byte array in the right way, i.e. taken from the SMS, but reversed, and then just access the TReal32.
René Brunner
Forum posts: 3
rbrunner... thanks for the idea. After pulling my hair out on this one... don't care about elegance. I will give that a try. Thanks again for the quick response. I will post back if it works or not
Forum posts: 3
So I found this other posting on the NewLC website... for moving between a TReal32 and a TDes8 for sending reals via Bluetooth. I will give this a try... looks like TLex8 may still be the way to go. I will give both the Union and TLex methods a try.
http://www.newlc.com/topic-956