[fpc-pascal] WORD (2 bytes) to String conversion

Paul Nicholls paulfnicholls at gmail.com
Sun Oct 25 23:42:18 CET 2009


----- Original Message ----- 
From: "M Pulis" <toothpic at fastq.com>
To: "FPC-Pascal users discussions" <fpc-pascal at lists.freepascal.org>
Sent: Monday, October 26, 2009 9:27 AM
Subject: Re: [fpc-pascal] WORD (2 bytes) to String conversion


>
> On Oct 25, 2009, at 3:09 PM, Paul Nicholls wrote:
>
>> ----- Original Message ----- From: "Graeme Geldenhuys" 
>> <graemeg.lists at gmail.com
>> >
>> To: "FPC-Pascal users discussions" <fpc-pascal at 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;
>
> Doesn't String[2] imply a length byte followed by 2 bytes for content  = 3 
> bytes for a Pascal string? Even a C string of two characters  content is 3 
> bytes.
>
> Just my $.03
>
> Gary

Hmm...good point!
I think you are right :)

So you would have to use this instead:

TMyHeader = Packed Record
  Case Integer Of
      0 : (ID     : Word);
      1 : (IDStr : Array[0..1] Of AnsiChar);
End;

cheers,
Paul 




More information about the fpc-pascal mailing list