编码

UnicodeString,AnsiString,WideString 和 UTF8String 等字符串类型使用各自的编码存储在内存中(有关详细信息,请参阅字符串类型)。将一种类型的字符串分配给另一种类型可能会导致转换。类型字符串旨在独立编码 - 你永远不应使用其内部表示。

Sysutils.TEncoding 提供方法 GetBytes 用于将 string 转换为 TBytes(字节数组),GetString 用于将 TBytes 转换为 stringSysutils.TEncoding 类还提供了许多预定义的编码作为类属性。

处理编码的一种方法是在应用程序中仅使用 string 类型,并在每次需要使用特定编码时使用 TEncoding - 通常在 I / O 操作,DLL 调用等中…

procedure EncodingExample;
var hello,response:string;
    dataout,datain:TBytes;
    expectedLength:integer;
    stringStream:TStringStream;
    stringList:TStringList;
     
begin
  hello := 'Hello World!Привет мир!';
  dataout := SysUtils.TEncoding.UTF8.GetBytes(hello); //Conversion to UTF8
  datain := SomeIOFunction(dataout); //This function expects input as TBytes in UTF8 and returns output as UTF8 encoded TBytes.
  response := SysUtils.TEncoding.UTF8.GetString(datain); //Convertsion from UTF8

  //In case you need to send text via pointer and length using specific encoding (used mostly for DLL calls)
  dataout := SysUtils.TEncoding.GetEncoding('ISO-8859-2').GetBytes(hello); //Conversion to ISO 8859-2
  DLLCall(addr(dataout[0]),length(dataout));
  //The same is for cases when you get text via pointer and length
  expectedLength := DLLCallToGetDataLength();
  setLength(datain,expectedLength);
  DLLCall(addr(datain[0]),length(datain));
  response := Sysutils.TEncoding.GetEncoding(1250).getString(datain);

   //TStringStream and TStringList can use encoding for I/O operations
   stringList:TStringList.create;
   stringList.text := hello;
   stringList.saveToFile('file.txt',SysUtils.TEncoding.Unicode);
   stringList.destroy;
   stringStream := TStringStream(hello,SysUtils.TEncoding.Unicode);
   stringStream.saveToFile('file2.txt');
   stringStream.Destroy;
end;