Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a null terminated string to string?

I want to store to disk some data records. I want to have each data record of equal size so I can quickly compute and jump to a certain record. So, I store the only string in my record as an array of chars:

type TFileNameIO = array[1..512] of Char;

After I read the string from disk, the content of my string is like this:

c:\windows#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0 etc

I want to truncate the #0 characters, and I do something like this:

function GetFileName(DiskName: TFileNameIO): string;
VAR PC: PChar;
begin
 SetString(Result, PChar(@DiskName), Length(DiskName));     <----- here Result is: c:\windows#0#0#0 etc
 PC:= PChar(Result);
 Result:= WideCharToString(PC);
end;

There is a better way to do it?

like image 330
Server Overflow Avatar asked Aug 22 '15 22:08

Server Overflow


2 Answers

You can assign a null-terminated PChar directly to a String:

function GetFileName(DiskName: TFileNameIO): string;
begin
 Result := PChar(@DiskName);
end;

Just make sure the TDiskName always contains a null terminator. If it can ever be completely full of characters without a null, you will have to do this instead:

function GetFileName(DiskName: TFileNameIO): string;
var
 Len, I: Integer;
begin
  Len := 0;
  For I := Low(DiskName) to High(DiskName) do
  begin
    if DiskName[I] = #0 then Break;
    Inc(Len);
  end;
  SetString(Result, PChar(@DiskName), Len);
end;
like image 73
Remy Lebeau Avatar answered Sep 28 '22 11:09

Remy Lebeau


Your function is not necessary because you can simply assign a character array to a string variable.

type
  TCharArray = array [1..512] of Char;
....
var
  arr: TCharArray;
  str: string;
....
arr := ...;
str := arr;

This results in str being assigned the contents of arr. The compiler implements this with a call to System._UStrFromWArray which first of all looks for a null-terminator. If it finds one, that determines the length of str. Otherwise, the entire contents of the array are copied, and Length(str)=Length(arr).

As a broader point, I would recommend that you change to using zero-based character arrays. From the documentation:

Zero-based character arrays are compatible with PChar and PWideChar. When you use a character array in place of a pointer value, the compiler converts the array to a pointer constant whose value corresponds to the address of the first element of the array.

This makes it easier for you to interop with functions that expect PChar arguments.

As an aside, passing TFileNameIO by value is inefficient because it involves a memory copy. Pass such types by reference. For instance as a const parameter.

like image 45
David Heffernan Avatar answered Sep 28 '22 10:09

David Heffernan