----- Original Message ----- From: "Graeme Geldenhuys" <graemeg.li...@gmail.com>
To: "FPC-Pascal users discussions" <fpc-pascal@lists.freepascal.org>
Sent: Friday, October 23, 2009 11:10 PM
Subject: [fpc-pascal] WORD (2 bytes) to String conversion


Hi,

I'm reading in a WORD (2 bytes) from a binary file. I can display the
Hex format of that value without a problem, but I would also like to
display the String value of that WORD variable. It's the first 2 bytes
of a file, which contains the "magic number" of the file.

I would like my program to output the following:

-----------------
Header Section
 header.ID    (5348h = "HS")
 ...
-----------------

Hi Graeme,
What about something like this (variant records, similar to C unions)? (untested, ie. from memory, but shoud work)

TMyHeader = Packed Record
   Case Integer Of
       0 : (ID     : Word);
       1 : (IDStr : String[2]);
End;

You should then be able to file read/write it identically, and also do this:

Var
   Header : TMyHeader;
Begin
   WriteLn(Header.ID) ;// as a word
   WriteLn(Header.IDStr); // as a 2 byte string
End;

cheers,
Paul
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to