Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

the fp2 data format


micrometeorology Aug 31, 2010 10:05 AM

Hello,
I have converted my tables from a CR3000 with LoggerNet to a TOB1 binary file. the datatypes of this binary file are: ULONG ULONG ULONG FP2 FP2. I read the TOB1 file with IDL as a bytearr. My question is now, how can i convert this FP2 types into a float? The FP2 data are 2 bytes long. I hope you can help me. Thank you


jtrauntvein Aug 31, 2010 01:11 PM

The FP2 data type is basically a decimal encoded floating point type. I use the following code in C++ to perform my conversions to floating point:

<pre>
typedef unsigned char byte;
typedef unsigned short uint2;
float csiFs2ToFloat(void const *buff_)
{
// we are going to cast the binary value into a two byte integer so that it is convenient to pick
// out patterns and pick off parts of the structure.
byte const *buff = static_cast<byte const *>(buff_);
uint2 fs_word = (uint2(buff[0]) << 8) + buff[1];

// we can now pick off the components of the FS2 structure
static uint2 const pos_infinity = 0x1fff;
static uint2 const neg_infinity = 0x9fff;
static uint2 const not_a_number = 0x9ffe;
bool is_negative = ((fs_word & 0x8000) != 0);
uint2 mantissa = fs_word & 0x1FFF;
uint2 exponent = (fs_word & 0x6000) >> 13;
double rtn;

if(fs_word == pos_infinity)
rtn = std::numeric_limits<float>::infinity();
else if(fs_word == neg_infinity)
rtn = -1.0f * std::numeric_limits<float>::infinity();
else if(fs_word == not_a_number)
rtn = std::numeric_limits<float>::quiet_NaN();
else
{
rtn = static_cast<float>(mantissa);

for(uint2 i = 0; mantissa != 0 && i < exponent; ++i)
rtn /= 10.0f;
if(is_negative && mantissa != 0)
rtn *= -1.0f;
}
return static_cast<float>(rtn);
} // csiFs2ToFloat

</pre>


Given that I know next to nothing about IDL (before this morning I knew nothing about the language), you will have to perform your own conversion. Hopefully, however, the function above should reveal all of the details which you may confront.

P.S, After posting the above, I discovered that all my lovely indentation was removed. Regardless, I hope that the code is still somewhat readable.

* Last updated by: jtrauntvein on 8/31/2010 @ 7:18 AM *

Log in or register to post/reply in the forum.