Board index » cppbuilder » I need a routine to convert a real48 type into a double

I need a routine to convert a real48 type into a double


2005-08-02 04:46:23 PM
cppbuilder95
Hi all,
I've got the following problem. I'm reading data out of a binary file which
had been written by a Delphi application.
The data structure of the delphi application which wrote the data looks like
this-->
TMyDelphiRecord = packed record
aReal48Value : Real48;
aByteValue : Byte;
aWordValue : Word;
and so on...
end;
My C++ Builder application reads the into a data structure like this
struct TMyCBuilderStruct{
unsigned char aReal48Value[6];
unsigned char aByteValue;
unsiged short aWordValue;
and so on...
};
My question ==>how can I convert the aReal48Value into a valid c++ double
value?.
Thanks in advance!
 
 

Re:I need a routine to convert a real48 type into a double

See if this does the trick:
orion.planet.de/~jan/Snippets.9707/_d0903.html
Your program is a 32 bit one and elements in a structure are aligned on 32
bit or 4-byte boundaries. You are reading in a packed structure, a
structure aligned on 8 bit or 1-byte boundaries. To do that you must
specify a packing size of 1 byte.
#pragma pack(1) /* set 1-byte alignment */
struct TMyCBuilderStruct {
unsigned char aReal48Value[6];
unsigned char aByteValue;
unsiged short aWordValue;
and so on...
};
#pragma pack() /* restore the original alignment */
Note that your question is about C++ Builder. However you have posted it
here, in the newsgroup for the old Borland C++ compiler.
Please post questions in newsgroups where what you are asking about is the
expected subject. You can identify which newsgroups are for C++ Builder by
the word 'cppbuilder' in their name.
. Ed
Quote
E. Kali wrote in message
news:42ef32d4$ XXXX@XXXXX.COM ...

I've got the following problem. I'm reading data out of a binary file
which had been written by a Delphi application.
The data structure of the delphi application which wrote the data looks
like this-->

TMyDelphiRecord = packed record
aReal48Value : Real48;
aByteValue : Byte;
aWordValue : Word;
and so on...
end;

My C++ Builder application reads the into a data structure like this

struct TMyCBuilderStruct{
unsigned char aReal48Value[6];
unsigned char aByteValue;
unsiged short aWordValue;
and so on...
};

My question ==>how can I convert the aReal48Value into a valid c++ double
value?.
 

Re:I need a routine to convert a real48 type into a double

I don't now if has a more easy way. I would create a function in .pas file
like...
unit XXX;
interface
type
TReal48__ = packed array[0..6] of byte;
function Real48ToDouble(value: TReal48__): Double;
implementation
function Real48ToDouble(value: TReal48__): Double;
type
PReal48 = ^Real48;
begin
result := PReal48(@value)^;
end;
end.
and use it...
Tales Aguiar.
"E. Kali" < XXXX@XXXXX.COM >wrote in message
Quote
Hi all,

I've got the following problem. I'm reading data out of a binary file
which
had been written by a Delphi application.
The data structure of the delphi application which wrote the data looks
like
this-->

TMyDelphiRecord = packed record
aReal48Value : Real48;
aByteValue : Byte;
aWordValue : Word;
and so on...
end;

My C++ Builder application reads the into a data structure like this

struct TMyCBuilderStruct{
unsigned char aReal48Value[6];
unsigned char aByteValue;
unsiged short aWordValue;
and so on...
};

My question ==>how can I convert the aReal48Value into a valid c++ double
value?.

Thanks in advance!








 

{smallsort}

Re:I need a routine to convert a real48 type into a double

Ooops
the array is wrong,
TReal48__ = packed array[0..5] of byte; // 5, no 6
"Tales Aguiar" < XXXX@XXXXX.COM >wrote in message
Quote
I don't now if has a more easy way. I would create a function in .pas file
like...

unit XXX;

interface

type
TReal48__ = packed array[0..6] of byte;

function Real48ToDouble(value: TReal48__): Double;

implementation

function Real48ToDouble(value: TReal48__): Double;
type
PReal48 = ^Real48;
begin
result := PReal48(@value)^;
end;

end.


and use it...

Tales Aguiar.


"E. Kali" < XXXX@XXXXX.COM >wrote in message
news:42ef32d4$ XXXX@XXXXX.COM ...
>Hi all,
>
>I've got the following problem. I'm reading data out of a binary file
which
>had been written by a Delphi application.
>The data structure of the delphi application which wrote the data looks
like
>this-->
>
>TMyDelphiRecord = packed record
>aReal48Value : Real48;
>aByteValue : Byte;
>aWordValue : Word;
>and so on...
>end;
>
>My C++ Builder application reads the into a data structure like this
>
>struct TMyCBuilderStruct{
>unsigned char aReal48Value[6];
>unsigned char aByteValue;
>unsiged short aWordValue;
>and so on...
>};
>
>My question ==>how can I convert the aReal48Value into a valid c++
double
>value?.
>
>Thanks in advance!
>
>
>
>
>
>
>
>


 

Re:I need a routine to convert a real48 type into a double

Quote
function Real48ToDouble(value: TReal48__): Double;
What is 'value'?
. Ed
Quote
Tales Aguiar wrote in message
news: XXXX@XXXXX.COM ...
I don't now if has a more easy way. I would create a function in .pas file
like...

unit XXX;

interface

type
TReal48__ = packed array[0..6] of byte;

function Real48ToDouble(value: TReal48__): Double;

implementation

function Real48ToDouble(value: TReal48__): Double;
type
PReal48 = ^Real48;
begin
result := PReal48(@value)^;
end;

end.


and use it...
 

Re:I need a routine to convert a real48 type into a double

Hi,
I've found the definition of the Real48 on the internet. So i wrote the
following function.
/***************************************************************************
* Function : ConvertReal48ToDouble
* Parameter(s) : szReal48Value - reald48 Value
* Return value : double - converted real 48 value
* Description : Converts a real48 value to double.
*
* NOTE: REAL48 format
* s f e
* bits 47 48...8 7..0
*
* s (e-129)
* v = (-1) * 2 * (1.f )
*
* If e = 0, then v = 0.
*
****************************************************************************/
double COxymonfilereader::ConvertReal48ToDouble(unsigned char
szReal48Value[6])
{
double fResult(0);
//strip the data out of real48 type
int E( (int) szReal48Value[0]);
int S( (int) szReal48Value[5] & 0x80);
int F(((int) szReal48Value & 0x7FFFF0)>>4);
if(E>0 && E<=255){
float f(0);
char szStrVal[10];
_snprintf(szStrVal, 10, "1.%d",F);
f = atof(szStrVal);
fResult = pow(-1.0,S) * pow(2.0,(E - 129)) * f;
}//if(E>0 and E<=255)
return fResult;
}
It seems to work!
What do you think about this.
"Ed Mulroy" < XXXX@XXXXX.COM >wrote in message
Quote
>function Real48ToDouble(value: TReal48__): Double;

What is 'value'?

. Ed

>Tales Aguiar wrote in message
>news: XXXX@XXXXX.COM ...
>I don't now if has a more easy way. I would create a function in .pas file
>like...
>
>unit XXX;
>
>interface
>
>type
>TReal48__ = packed array[0..6] of byte;
>
>function Real48ToDouble(value: TReal48__): Double;
>
>implementation
>
>function Real48ToDouble(value: TReal48__): Double;
>type
>PReal48 = ^Real48;
>begin
>result := PReal48(@value)^;
>end;
>
>end.
>
>
>and use it...


 

Re:I need a routine to convert a real48 type into a double

----- Original Message -----
From: "Ed Mulroy" < XXXX@XXXXX.COM >
Newsgroups: borland.public.cpp.borlandcpp
Sent: Friday, August 05, 2005 19:02
Subject: Re: I need a routine to convert a real48 type into a double
Quote
>function Real48ToDouble(value: TReal48__): Double;

What is 'value'?

function argument of type TReal48__...