[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Converting Turbo Pascal 7.0 structures with real to C++

Posted on 1998-11-09
4
Medium Priority
?
441 Views
Last Modified: 2008-03-17
I am writing a Visual C++ program which converts data which was stored in a Turbo Pascal 7.0 structure containing character data and reals and written directly to disk.  How do you convert the 6-bytes real Pascal number to a usable C++ floating point number?

Turbo pascal structure stored on disk :

memrec = record
       ItWht : real
end;

Visual C++ record using CFile to read

typedef struct test
{
      BYTE ItWht[6];
};
0
Comment
Question by:blevine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 800 total points
ID: 1177393
Hi,

Below are some pascal declarations and code that will transform a 6 byte TP real to an 8 byte IEEE real.

Translating this into C should be pretty simple...

Cheers,

Raymond.

     treal6 = record
              case boolean of
              true  : (value : real);
              false : (exp : byte ; mant2 : byte5);
              end;

     real8 = record
              case boolean of
              true  : (byteval : byte8);
              false : (mant1 : integer; mant2 : byte5; exp : byte);
              end;

     turbo86 = record
             case boolean of
              true : (tval : byte6);
              false :(rval : real);
             end;
     turbo87 = record
             case boolean of
              true : (tval : byte8);
              false: (rval : real);
             end;

procedure conv86_87(real86 : turbo86;var real87 : turbo87);
{ convert a turbo86 real number to a turbo87 real number }
var
   i,j,newexp : integer;
   sign : byte;
begin
{$IFOPT R+} {$DEFINE Rchk2} {$R-} {$ENDIF}

if real86.tval[1] = 0 then newexp := 0
else
   newexp := (real86.tval[1] + 894) shl 4;
sign := real86.tval[6] and 128; { extract sign bit }
real87.tval[8] := sign or (hi(newexp) and 127);
real87.tval[7] := (lo(newexp) and 240) or ((real86.tval[6] shr 3) and 15);
for i := 5 downto 2 do
   real87.tval[i+1] := (real86.tval[i+1] shl 5) or (real86.tval[i] shr 3);
real87.tval[2] := (real86.tval[2] shl 5) and $ff;
real87.tval[1] := 0;
{$IFDEF Rchk} {$UNDEF Rchk2} {$R+} {$ENDIF}
end;

{------------------------------------------------------------------------}
PROCEDURE real_to_double(the_real : system.real; VAR the_double : byte8);
VAR
  turbo86_buffer    : turbo86 ABSOLUTE the_real;
  turbo87_buffer    : turbo87 ABSOLUTE the_double;
 
BEGIN
  conv86_87(turbo86_buffer, turbo87_buffer);
END;

0
 

Author Comment

by:blevine
ID: 1177394
Pardon my stupidity, but are you saying that I should use the turb86 conversion of the value real86 and the real87 number resulting from it will be an 8-byte C++ float representation?  Then I can just cast it as a float and get the number?

if real86.tval[1] = 0 then newexp := 0
else
   newexp := (real86.tval[1] + 894) shl 4;
sign := real86.tval[6] and 128; { extract sign bit }
real87.tval[8] := sign or (hi(newexp) and 127);
real87.tval[7] := (lo(newexp) and 240) or ((real86.tval[6] shr 3) and 15);
for i := 5 downto 2 do
   real87.tval[i+1] := (real86.tval[i+1] shl 5) or (real86.tval[i] shr 3);
real87.tval[2] := (real86.tval[2] shl 5) and $ff;
real87.tval[1] := 0;

0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1177395
Yes.

real87 will contain the 8 byte IEEE representation of the 6 byte TP real. Casting it to a double will do what you want.

Raymond.
0
 

Author Comment

by:blevine
ID: 1177396
Hi, I just added another question for you about the code you supplied me.......
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question