Board index » delphi » Writing real numbers to file in hex format

Writing real numbers to file in hex format

I need to take 'real' numbers and write them to a file in hexadecimal
format, which should be 8 hexadecimal characters.  I cannot find any
documentation on how to do this from Borland's TP6 online or hardcopy
documentation. I can read them, but ...

Any ideas would be greatly appreciated.

Dale Stanbridge
dal...@ix.netcom.com

 

Re:Writing real numbers to file in hex format


Quote
Dale Stanbridge wrote:

> I need to take 'real' numbers and write them to a file in hexadecimal
> format, which should be 8 hexadecimal characters.  I cannot find any
> documentation on how to do this from Borland's TP6 online or hardcopy
> documentation. I can read them, but ...

> Any ideas would be greatly appreciated.

     If what you mean is that you want to write out the hex
representation of the real number's internal storage, then you want to
use the Pascal "union" record type, and represent the number as either
a real or an array of 8 bytes.

  CONST
   bytesperreal = 8;
  TYPE
   bytetype = 0 .. 255;
   byteindextype = 1 .. bytesperreal;
   realbytes = array [byteindextype] of bytetype;
   numerictype = RECORD
    CASE format (onereal, eightbytes) OF
     onereal : (realnum : real);
     eightbytes : (bytearray : realbytes)
    END;
   VAR
    numeric : numerictype;
    byteindex : byteindextype;

...

   WITH numeric DO
    BEGIN
     onereal := 3.14159;
     FOR byteindex := 1 to 8 do writehex (eightbytes[byteindex])
    END;

Bob Schor
Pascal Enthusiast

Re:Writing real numbers to file in hex format


In article <5o6sr5$...@dfw-ixnews10.ix.netcom.com> of Tue, 17 Jun 1997
20:42:13 in comp.lang.pascal.borland, Dale Stanbridge

Quote
<dal...@ix.netcom.com> wrote:
>I need to take 'real' numbers and write them to a file in hexadecimal
>format, which should be 8 hexadecimal characters.  I cannot find any
>documentation on how to do this from Borland's TP6 online or hardcopy
>documentation. I can read them, but ...

UNTESTED, but from memory of tested code -

function H2(const B : byte) : string ;
const HC : array [0..15] of char = '0123456789ABCDEF' ;
      S : string [2] = 'xx' ;
begin S[1] := HC[B shr 4] ; S[2] := HC[B and $0F] ; H2 := S end ;

function H4(const W : word) : string ;
begin H4 := H2(Hi(W))+H2(Lo(W)) end ;

type X = record H, L : word end ;

function H8(const LI : longint) : string ;
begin with X(LI) do H8 := H4(H)+H4(L) end ;

OR something like

function Hex(const L : longint) : string ;
begin if L=0 then Hex := '$' else Hex := Hex(L shr 4)+HC[L and $F] end ;

which, unlike the previous, suppresses leading zeroes but puts '$' in
front.

From these clues ...
--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web URL: http://www.merlyn.demon.co.uk/ -- includes FAQqish topics and links.
  Correct 4-line sig separator is as above, a line comprising "-- " (SoRFC1036)
  Before a reply, quote with ">" / "> ", known to good news readers (SoRFC1036)

Re:Writing real numbers to file in hex format


In article <33A7395A.4...@vms.cis.pitt.edu>,
Bob Schor  <bsc...@vms.cis.pitt.edu> wrote:

Quote
>Dale Stanbridge wrote:

>> I need to take 'real' numbers and write them to a file in hexadecimal
>> format, which should be 8 hexadecimal characters.  I cannot find any
>> documentation on how to do this from Borland's TP6 online or hardcopy
>> documentation. I can read them, but ...

>> Any ideas would be greatly appreciated.

>     If what you mean is that you want to write out the hex
>representation of the real number's internal storage, then you want to
>use the Pascal "union" record type, and represent the number as either
>a real or an array of 8 bytes.

>  CONST
>   bytesperreal = 8;
>  TYPE
>   bytetype = 0 .. 255;
>   byteindextype = 1 .. bytesperreal;
>   realbytes = array [byteindextype] of bytetype;
>   numerictype = RECORD
>    CASE format (onereal, eightbytes) OF

one needs a colon after format. However, as on this case one does not
need to store what field is used, one can just do case boolean of...

There are other methods of achieving same result like typecasting and
absolute variables.

Osmo

Other Threads