Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-10

WORD (2 bytes) to String conversion

22 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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/
Graeme Geldenhuys
to Graeme Geldenhuys

2009/10/23 Graeme Geldenhuys :

Hi, I'm reading in a WORD (2 bytes) from a binary file. I can display the

As I pressed the Send button, I thought about trying the following. And it worked! But is this the best way of doing what I want?

type
  TWord = record
    b1: Char;
    b2: Char;
  end;
var
  w: TWord;
  s: string;
begin
  w := TWord(hdr^.ID);
  s := w.b1 + w.b2;
  writeln(s);
end;
Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/
Aleksa Todorovic
to Graeme Geldenhuys: I'm reading in a WORD (2 bytes) from a binary file. I can display the Hex format of that…
Graeme Geldenhuys wrote 14 lines
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)

Show quoted text 14 lines
-- Regards,  - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/ _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Aleksa Todorovic - Lead Programmer Eipix Entertainment http://www.eipix.com/
Graeme Geldenhuys
to Aleksa Todorovic

2009/10/23 Aleksa Todorovic :

Show quoted text 6 lines
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/
Jeff Wormsley
to Graeme Geldenhuys: I'm reading in a WORD (2 bytes) from a binary file. I can display the Hex format of that…
Graeme Geldenhuys wrote 7 lines
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.
Graeme Geldenhuys
to Jeff Wormsley
Jeff Wormsley 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/
Graeme Geldenhuys
to Jeff Wormsley
Jeff Wormsley 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/
Graeme Geldenhuys
to Jürgen Hestermann
Jürgen Hestermann 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/
Vincent Snijders
to Graeme Geldenhuys
Graeme Geldenhuys wrote 15 lines
On 23/10/2009, Jürgen Hestermann 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

Marco van de Voort
to Vincent Snijders

In our previous episode, Vincent Snijders said:

Show quoted text 7 lines
> 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.

(starting to use ansichar might not be bad)

Graeme Geldenhuys
to Vincent Snijders: You can consider declaring the ID in the header record type as array[1..2] of char.…
Vincent Snijders 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/
Graeme Geldenhuys
to Marco van de Voort: In our previous episode, Vincent Snijders said: (starting to use ansichar might not be…
Marco van de Voort wrote 5 lines
> > You can consider declaring the ID in the header record type as > array[1..2] of char. (starting to use ansichar might not be bad)

Good point. But I preferred to go with array of Byte.

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/
Vincent Snijders
to Graeme Geldenhuys: As Marco suggested... Char size might not always be the same. I did however change my…
Graeme Geldenhuys wrote 9 lines
On 23/10/2009, Vincent Snijders 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

Graeme Geldenhuys
to Vincent Snijders

2009/10/24 Vincent Snijders :

Show quoted text 7 lines
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/
Vincent Snijders
to Graeme Geldenhuys
Graeme Geldenhuys wrote 19 lines
2009/10/24 Vincent Snijders : > 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

Marco van de Voort
to Jürgen Hestermann

In our previous episode, Jürgen Hestermann said:

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

Codegear did this with Delphi 2009 in August 2008, forcing to check all string code.

However FPC will probably only add a Delphi2009 mode, and code in the current modes will remain working.

Paul Nicholls
to Graeme Geldenhuys: I'm reading in a WORD (2 bytes) from a binary file. I can display the Hex format of that…

----- Original Message -----

Earlier message 42 lines
From: "Graeme Geldenhuys" To: "FPC-Pascal users discussions" 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
M Pulis
to Paul Nicholls
Paul Nicholls wrote 32 lines
----- Original Message ----- From: "Graeme Geldenhuys" < > To: "FPC-Pascal users discussions" 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

Paul Nicholls
to M Pulis

----- Original Message -----

Earlier message 63 lines
From: "M Pulis" To: "FPC-Pascal users discussions" 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" >> < >> > >> To: "FPC-Pascal users discussions" >> 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