編碼
UnicodeString,AnsiString,WideString 和 UTF8String 等字串型別使用各自的編碼儲存在記憶體中(有關詳細資訊,請參閱字串型別)。將一種型別的字串分配給另一種型別可能會導致轉換。型別字串旨在獨立編碼 - 你永遠不應使用其內部表示。
類 Sysutils.TEncoding
提供方法 GetBytes
用於將 string
轉換為 TBytes
(位元組陣列),GetString
用於將 TBytes
轉換為 string
。Sysutils.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;