Delphi Basics
WideCharToString
Function
Copies a null terminated WideChar string to a normal string System unit
 function WideCharToString ( Source : PWideChar ) : string;
Description
The WideCharToString function converts a null terminated WideChar string or array to a normal string.
 
WideChar's are 2 bytes in size, to accomodate International character sets, such as Chinese, where the number of characters exceeds 256.
 
Such a double byte to single byte conversion will preserve the double byte size when it cannot be mapped to a single byte. So a 10 character WideChar string will yield anything from 10 to 20 characters in the resulting string.
Related commands
StringToWideChar Converts a normal string into a WideChar 0 terminated buffer
 
Example code : A simple example
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  // The System unit does not need to be defined
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  wideCharArray : array[0..5] of WideChar;
  myString      : String;

begin
  // Set up our WideChar array
  wideCharArray[0] := 'H';
  wideCharArray[1] := 'e';
  wideCharArray[2] := 'l';
  wideCharArray[3] := 'l';
  wideCharArray[4] := 'o';
  wideCharArray[5] := #0;   // Terminates WideChar strings

  // Copy to a normal string
  myString := WideCharToString(wideCharArray);

  // Show what the copy gave
  ShowMessage(myString);
end;
 
end.
Hide full unit code
   Hello
 
 
© CodeGear 2006 - 2007. All rights reserved.  |  Home Page