Board index » delphi » nul characters inside a string

nul characters inside a string

I am new to pascal and trying to write a program to unravel a binary file
containing nul (#0) characters.  The problem is that whenever i read this
into a string, delphi sees the nul character and terminates the string.

What is the best way to break the input data into its "field" components.
I have worked out that the layout contains a date followed by 5 number
fields.  I've been using   xString := copy(rec,5,4);  Unfortunately
character 5 contains #0 so xString
becomes an empty string.

Alan in KL.

 

Re:nul characters inside a string


Quote
"Alan Hughston" <hu...@pc.jaring.my> wrote:
>I am new to pascal and trying to write a program to unravel a binary file
>containing nul (#0) characters.  The problem is that whenever i read this
>into a string, delphi sees the nul character and terminates the string.
>What is the best way to break the input data into its "field" components.
>I have worked out that the layout contains a date followed by 5 number
>fields.  I've been using   xString := copy(rec,5,4);  Unfortunately
>character 5 contains #0 so xString
>becomes an empty string.
>Alan in KL.

The Copy() routine shouldn't care about Null characters, something
else must be going on.  

From your description I'm not sure you're entirely clear on reading
from a binary file (any of which will almost undoubtedly contain
"null" characters for numerous reasons), and working with the fields
in a record (which I assume is what "rec" is).  

Can you post or Email me a bit more code to give some more context to
the problem?  F'rinstance, how is rec declared, how is xString
declared (and are you using D1 or D2? and if D2: Short or Long
strings)?

HTH

Stephen Posey
slpo...@concentric.net

Re:nul characters inside a string


Quote
> I am new to pascal and trying to write a program to unravel a binary file
> containing nul (#0) characters.  The problem is that whenever i read this
> into a string, delphi sees the nul character and terminates the string.

If you're reading blocks of binary data from a file, your code should look
something like this:

var
  F : File;   // <- Not Text
  Data : Array[0..5] of Byte;
  BytesReadIn : Integer;

begin
  F := AssignFile(F,'FileName.bin');
  Reset(F,1);       // The 1 here indicates the size of each record to read
in.
                         // You may want to adjust this if you're reading
in blocks of
                         // data that are all the same size.
  try
    Repeat
      BlockRead(F,Data,Sizeof(Data),BytesReadIn);
       // Do something with Data[0..BytesReadIn-1] here
    Until BytesReadIn <> Sizeof(Data);
  finally
    CloseFile(F);
  end;
end;

Re:nul characters inside a string


Hi,

Quote
Alan Hughston wrote:

> I am new to pascal and trying to write a program to unravel a binary file
> containing nul (#0) characters.  The problem is that whenever i read this
> into a string, delphi sees the nul character and terminates the string.

> What is the best way to break the input data into its "field" components.
> I have worked out that the layout contains a date followed by 5 number
> fields.  I've been using   xString := copy(rec,5,4);  Unfortunately
> character 5 contains #0 so xString
> becomes an empty string.

> Alan in KL.Alan, I suggest you use a PChar 'buffer' or similar as Pascal's string

handling is not really for binary 'blob' manipulation.

Shaun

Re:nul characters inside a string


On Thu, 01 May 1997 22:29:46 +0930, Shaun Cunniffe

Quote
<sha...@dubloo.com.au> wrote:
>Alan Hughston wrote:

>> I am new to pascal and trying to write a program to unravel a binary file
>> containing nul (#0) characters.  The problem is that whenever i read this
>> into a string, delphi sees the nul character and terminates the string.

No, only the view in the de{*word*81} shows that.  In fact, all the
characters are still there.

Quote
>Alan, I suggest you use a PChar 'buffer' or similar as Pascal's string
>handling is not really for binary 'blob' manipulation.

A buffer is a good idea, but a PChar is not.  The convention with
PChar is that a null character signals the end of the string.

Duncan Murdoch

Other Threads