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

2009-10-25 Thread Paul Nicholls
- 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


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

2009-10-25 Thread M Pulis


On Oct 25, 2009, at 3:09 PM, Paul Nicholls wrote:

- 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;


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
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2009-10-24 Thread Vincent Snijders

Graeme Geldenhuys schreef:

On 23/10/2009, Vincent Snijders vsnijd...@vodafonevast.nl wrote:

 You can consider declaring the ID in the header record type as array[1..2]
of char.


As Marco suggested... Char size might not always be the same. I did
however change my header to array[0..1] of byte instead.





But then you still cannot do:

if header.id='HS' then
  // magic is correct.

That is why I proposed a static array of char.

Vincent

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2009-10-24 Thread Graeme Geldenhuys
2009/10/24 Vincent Snijders vsnijd...@vodafonevast.nl:

 But then you still cannot do:

 if header.id='HS' then
  // magic is correct.

 That is why I proposed a static array of char.


No, but I can do this... which is sufficient.

var
  s: string
begin
  s := hdr^.ID;
  if S = HS then
...


-- 
Regards,
  - Graeme -


___
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2009-10-24 Thread Vincent Snijders

Graeme Geldenhuys schreef:

2009/10/24 Vincent Snijders vsnijd...@vodafonevast.nl:

But then you still cannot do:

if header.id='HS' then
 // magic is correct.

That is why I proposed a static array of char.



No, but I can do this... which is sufficient.

var
  s: string
begin
  s := hdr^.ID;
  if S = HS then
...




Not with fpc, I guess, otherwise I learned two new features:
* string constants that are surrounded with double quotes
* assignment to static array of byte to a string without conversion

What type is ID?

Vincent
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2009-10-24 Thread Jürgen Hestermann
As Marco suggested... Char size might not always be the same. 


Why that? I hope noone changes the meaning of such long-time types some 
day. If a new type is needed, then it should get a new name too.


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2009-10-23 Thread Graeme Geldenhuys
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)
  ...
-


-- 
Regards,
  - Graeme -


___
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2009-10-23 Thread Aleksa Todorovic
On Fri, Oct 23, 2009 at 14:10, Graeme Geldenhuys
graemeg.li...@gmail.com wrote:
 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)
  ...
 -


w: WORD
...
YourString := Chr(Lo(w)) + Chr(Hi(w))
or
YourString := Chr(Hi(w)) + Chr(Lo(w))

(depending on endianness)





 --
 Regards,
  - Graeme -


 ___
 fpGUI - a cross-platform Free Pascal GUI toolkit
 http://opensoft.homeip.net/fpgui/
 ___
 fpc-pascal maillist  -  fpc-pas...@lists.freepascal.org
 http://lists.freepascal.org/mailman/listinfo/fpc-pascal




-- 
Aleksa Todorovic - Lead Programmer
Eipix Entertainment
http://www.eipix.com/
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2009-10-23 Thread Graeme Geldenhuys
2009/10/23 Aleksa Todorovic alexi...@gmail.com:

 w: WORD
 ...
 YourString := Chr(Lo(w)) + Chr(Hi(w))
 or
 YourString := Chr(Hi(w)) + Chr(Lo(w))


Ah, thanks. I was close, I used High() and Low() instead. :-( Dope!



-- 
Regards,
  - Graeme -


___
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2009-10-23 Thread Jeff Wormsley


Graeme Geldenhuys wrote:

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.
  
Personally, I'd do something like this, as it is more explicit (you 
won't be left wondering what magic occurred later:


function WordToString(w: word): String;
begin
Result := Char(hi(w)) + Char(lo(w));
end;

That's dangerous, though, if your magic number's two bytes aren't 
printable ASCII.  I believe there is an RTL function like 
IsPrintableASCII or something like that that you could check each 
character with. 


Jeff.

--
I haven't smoked for 3 years, 2 months and 6 days, saving $5,235.38 and 
not smoking 34,902.58 cigarettes.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2009-10-23 Thread Graeme Geldenhuys
On 23/10/2009, Jeff Wormsley dawor...@comcast.net wrote:

  That's dangerous, though, if your magic number's two bytes aren't printable

That should never be a problem for my tool though. The tool I am
writing is specific to the INF help format. The tool dumps the file
structure to a text file. (I need this for debugging purposes)

When my app processes a file, it reads the first 155 bytes (header
information), then does a numerical check of the magic number in the
header. If it's an unknown value, it stops right there. If it's a
known value, it continues at which point it starts outputing the text
to file.


-- 
Regards,
  - Graeme -


___
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2009-10-23 Thread Jeff Wormsley





Graeme Geldenhuys wrote:

  On 23/10/2009, Jeff Wormsley dawor...@comcast.net wrote:
  
  
 That's dangerous, though, if your magic number's two bytes aren't printable

  
  
That should never be a problem for my tool though. The tool I am
writing is specific to the INF help format. The tool dumps the file
structure to a text file. (I need this for debugging purposes)

Oh, I was fairly certain of that.  Its just these little routines tend
to end up in libraries of handy code.  I tend to be overly cautious
with some of these because of that.

Jeff.

--
I haven't smoked for 3 years, 2 months and 6 days, saving $5,235.76 and
not smoking 34,905.07 cigarettes.


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

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

2009-10-23 Thread Graeme Geldenhuys
On 23/10/2009, Jeff Wormsley dawor...@comcast.net wrote:
  Oh, I was fairly certain of that.  Its just these little routines tend to
 end up in libraries of handy code.  I tend to be overly cautious with some
 of these because of that.

Point taken. :-)  This code is purposely rewritten and totally
standalone so I can track down a bug in my actual project, the INF
DocView project.


-- 
Regards,
  - Graeme -


___
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2009-10-23 Thread Jürgen Hestermann

I would like my program to output the following:
-
Header Section
  header.ID(5348h = HS)
  ...
-


Wouldn't it be more direct to use

Blockwrite(file,header.ID,sizeof(header.ID));

That would make it robust against changes of ID size and would avoid the 
many type castings.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2009-10-23 Thread Graeme Geldenhuys
On 23/10/2009, Jürgen Hestermann juergen.hesterm...@gmx.de wrote:

  Wouldn't it be more direct to use
  Blockwrite(file,header.ID,sizeof(header.ID));

I read the complete header structure in one go (155 bytes). I simply
want to output to a text file, a human readable structure of the
complete INF file. The INF file format is quite complex and one can
easily get confuse between various bits of information all in various
locations inside the file.

The tool I am writing is imply so I can see in a more visual way the
various bits of information, without the need for a hex editor etc...
So the solution described earlier will suffice for my needs.


-- 
Regards,
  - Graeme -


___
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2009-10-23 Thread Vincent Snijders

Graeme Geldenhuys schreef:

On 23/10/2009, Jürgen Hestermann juergen.hesterm...@gmx.de wrote:

 Wouldn't it be more direct to use
 Blockwrite(file,header.ID,sizeof(header.ID));


I read the complete header structure in one go (155 bytes). I simply
want to output to a text file, a human readable structure of the
complete INF file. The INF file format is quite complex and one can
easily get confuse between various bits of information all in various
locations inside the file.

The tool I am writing is imply so I can see in a more visual way the
various bits of information, without the need for a hex editor etc...
So the solution described earlier will suffice for my needs.




You can consider declaring the ID in the header record type as 
array[1..2] of char.


Vincent
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2009-10-23 Thread Graeme Geldenhuys
On 23/10/2009, Vincent Snijders vsnijd...@vodafonevast.nl wrote:

  You can consider declaring the ID in the header record type as array[1..2]
 of char.

As Marco suggested... Char size might not always be the same. I did
however change my header to array[0..1] of byte instead.



-- 
Regards,
  - Graeme -


___
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal