Board index » delphi » Null Terminated strings

Null Terminated strings

Let's say you are using a dbf file . A field there is stored in binary. When
Delphi sees #0 it terminates the string. If you open with dos utilities you
will see that this field contains more characters than it shows Delphi.

For example

 Edit1.text ;= 'Show' + #0 + 'Message';
ShowMessage(Edit1.Text);     // You will see only 'Show';

Is there any way to retrieve or omit null terminated strings?

 

Re:Null Terminated strings


Delphi strings can store null characters, but Windows is not able to display
them. Use something like:

function GetDisplayStr(const S: String): String;
var
  i: Integer;
begin
  Result := S;
  for i := 1 to Length(Result) do
    if Result[i] = #0 then
      Result[i] := ' ';
end;

"Thanasis Bratsas" <t_brat...@sournopoulos.gr> schreef in bericht
news:8j7bg1$fmm$1@medousa.forthnet.gr...

Quote

> Let's say you are using a dbf file . A field there is stored in binary.
When
> Delphi sees #0 it terminates the string. If you open with dos utilities
you
> will see that this field contains more characters than it shows Delphi.

> For example

>  Edit1.text ;= 'Show' + #0 + 'Message';
> ShowMessage(Edit1.Text);     // You will see only 'Show';

> Is there any way to retrieve or omit null terminated strings?

Re:Null Terminated strings


Im Artikel <8j7bg1$fm...@medousa.forthnet.gr>, "Thanasis Bratsas"
<t_brat...@sournopoulos.gr> schreibt:

Quote
> Edit1.text ;= 'Show' + #0 + 'Message';
>ShowMessage(Edit1.Text);     // You will see only 'Show';

>Is there any way to retrieve or omit null terminated strings?

In your example you the display is truncated not by Delphi, but instead by the
control and the Windows procedures behind the scene.

You should separate the *retrieval* of a string from the *display*. Whenever
you retrieve a string from some data source (file...), you must not only
provide a starting position for the string, but also a length indication, i.e.
either the exact length of the string, or a terminating character. Delphi
provides support for both string types, where PChar strings are terminated by a
Null character, whereas dedicated string types (string, ShortString,
AnsiString, WideString) include the character count in the data part of such
variables.

When you want to store some string in a Delphi variable, you must retrieve the
text as appropriate to the data source. In DBF files AFAIR all fields have a
fixed size, as defined in the database declaration. But a text field can hold
less *valid* characters than indicated by the field with, in which case a Null
character indicates the premature end of the contained string. Here a PChar
will be the appropriate "string" reference, but a maximum length also is
required, when the field is completely filled with characters, so that no
terminating character can be appended to the text.

When you want to display a Delphi string in a Windows control, you must replace
any terminating (Null) character inside the valid portion of the string by
something else, which will prevent the control from cutting the string at the
point, where it finds (in its opinion) a terminating character.

DoDi

Other Threads