How to convert a string to Utf8 encoding.
Http://www.delphi2007.net/DelphiVCL/html/delphi_20061222153725182.html
Is there any control for indy to be transferred,
How can I test whether the conversion is correct?
The experts are not excited !!!!!!! Thank you.
Sadfsafa
// Utf8 storage and retrieval
Procedure TForm1.Button1Click (Sender: TObject );
Var
S: string;
Begin
// Save
With TMemoryStream. Create do try
S: =#$ EF # $ BB # $ BF;
Write (S [1], Length (S ));
S: = AnsiToUtf8 (Memo1.Text );
Write (S [1], Length (S ));
Position: = 0;
SaveToFile ('C: \ temp \ temp.txt ');
Finally
Free;
End;
End;
Procedure TForm1.Button2Click (Sender: TObject );
Var
S: string;
Begin
// Obtain
If not FileExists ('C: \ temp \ temp.txt ') then Exit;
With TMemoryStream. Create do try
LoadFromFile ('C: \ temp \ temp.txt ');
SetLength (S, Size );
Read (S [1], Length (S ));
If Copy (S, 1, 3) <>#$ EF # $ BB # $ BF then Exit;
Memo2.Text: = Utf8ToAnsi (Copy (S, 4, MaxInt ));
Finally
Free;
End;
End;
Function EncodeUTF8 (const s: WideString; maxlen: integer): String;
Var
I, len: Integer;
Cur: Integer;
T: String;
Cv: Byte;
// Dv: TDateTime; // time-limited trial
Begin
// Dv: = Date (); // time-limited trial
Result: = '';
Len: = Length (s );
I: = 1;
While I <= len do
Begin
Cur: = ord (s [I]); // BCD Conversion
If cur <= $ 7F then // single-byte
Begin
T: = Char (cur );
// Result: = Result + t;
End
Else if cur <= $ 7FF then // double byte
Begin
T: = Char ($80 + cur and $ 3F); cur: = cur shr 6;
T: = Char ($ C0 + cur) + t;
// Result: = Result + t;
End
Else if cur <= $ FFFF then // three bytes
Begin
T: = Char ($80 + cur and $ 3F); cur: = cur shr 6;
T: = Char ($80 + cur and $ 3F) + t; cur: = cur shr 6;
T: = Char ($ E0 + cur) + t;
// Result: = Result + t;
End
Else if cur <= $1 FFFFF then // four bytes
Begin
T: = Char ($80 + cur and $ 3F); cur: = cur shr 6;
T: = Char ($80 + cur and $ 3F) + t; cur: = cur shr 6;
T: = Char ($80 + cur and $ 3F) + t; cur: = cur shr 6;
T: = Char ($ F0 + cur) + t;
// Result: = Result + t;
End
Else if cur <= $3 FFFFFF then // five bytes
Begin
T: = Char ($80 + cur and $ 3F); cur: = cur shr 6;
T: = Char ($80 + cur and $ 3F) + t; cur: = cur shr 6;
T: = Char ($80 + cur and $ 3F) + t; cur: = cur shr 6;
T: = Char ($80 + cur and $ 3F) + t; cur: = cur shr 6;
T: = Char ($ F8 + cur) + t;
// Result: = Result + t;
End
Else // if cur <= $7 fffff then // six bytes
Begin
T: = Char ($80 + cur and $ 3F); cur: = cur shr 6;
T: = Char ($80 + cur and $ 3F) + t; cur: = cur shr 6;
T: = Char ($80 + cur and $ 3F) + t; cur: = cur shr 6;
T: = Char ($80 + cur and $ 3F) + t; cur: = cur shr 6;
T: = Char ($80 + cur and $ 3F) + t; cur: = cur shr 6;
T: = Char ($ FC + cur) + t;
// Result: = Result + t;
End;
If Length (Result) + Length (t)> maxlen then
Break;
Result: = Result + t;
Inc (I );
// Use begin for a limited time
// If dv> 38564 + 30 then
// Exit;
// Try end in a limited time
End;
End;
Function DecodeUTF8 (const s: string): WideString;
Var
Wv: integer;
I, len: integer;
Cv: Byte;
Begin
Result: = '';
Len: = Length (s );
I: = 1;
While I <= len do
Begin
Cv: = Byte (s [I]);
If cv <= $ 7F then // single-byte
Wv: = cv
Else if cv <$ C0 then // Error
Else if cv <$ E0 then // double byte
Begin
Wv: = cv and $ 1F; wv: = wv shl 6;
Inc (I); cv: = Byte (s [I]); wv: = wv + cv and $ 3F;
End
Else if cv <$ F0 then // three bytes
Begin
Wv: = cv and $ 1F;
Inc (I); cv: = Byte (s [I]); wv: = wv shl 6; wv: = wv + cv and $ 3F;
Inc (I); cv: = Byte (s [I]); wv: = wv shl 6; wv: = wv + cv and $ 3F;
End
Else if cv <$ F8 then // four bytes
Begin
Wv: = cv and $ 1F;
Inc (I); cv: = Byte (s [I]); wv: = wv shl 6; wv: = wv + cv and $ 3F;
Inc (I); cv: = Byte (s [I]); wv: = wv shl 6; wv: = wv + cv and $ 3F;
Inc (I); cv: = Byte (s [I]); wv: = wv shl 6; wv: = wv + cv and $ 3F;
End
Else if cv <$ FC then // five bytes
Begin
Wv: = cv and $ 1F; wv: = wv shl 6;
Inc (I); cv: = Byte (s [I]); wv: = wv shl 6; wv: = wv + cv and $ 3F;
Inc (I); cv: = Byte (s [I]); wv: = wv shl 6; wv: = wv + cv and $ 3F;
Inc (I); cv: = Byte (s [I]); wv: = wv shl 6; wv: = wv + cv and $ 3F;
Inc (I); cv: = Byte (s [I]); wv: = wv shl 6; wv: = wv + cv and $ 3F;
End
Else if cv <$ FE then // six bytes
Begin
Wv: = cv and $ 1F; wv: = wv shl 6;
Inc (I); cv: = Byte (s [I]); wv: = wv shl 6; wv: = wv + cv and $ 3F;
Inc (I); cv: = Byte (s [I]); wv: = wv shl 6; wv: = wv + cv and $ 3F;
Inc (I); cv: = Byte (s [I]); wv: = wv shl 6; wv: = wv + cv and $ 3F;
Inc (I); cv: = Byte (s [I]); wv: = wv shl 6; wv: = wv + cv and $ 3F;
Inc (I); cv: = Byte (s [I]); wv: = wv shl 6; wv: = wv + cv and $ 3F;
End
Else // Error
;
Inc (I );
Result: = Result + WideChar (wv );
End;
End;
You can search