sadfsafa
//Utf8存、取
procedure TForm1.Button1Click(Sender: TObject); var S: string; begin //存 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 //取 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;// 限时试用 begin // dv := Date();// 限时试用 Result:=''; len:=Length(s); i:=1; while i <= len do begin cur := ord(s[i]); //BCD转换 if cur <= $7F then //单字节 begin t := Char(cur); // Result := Result + t; end else if cur <= $7FF then //双字节 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 //三字节 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 <= $1FFFFF then //四字节 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 <= $3FFFFFF then //五字节 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 <= $7FFFFFFF then //六字节 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); // 限时试用begin // if dv > 38564 + 30 then // Exit; // 限时试用end 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 // 单字节 wv := cv else if cv < $C0 then // Error else if cv < $E0 then // 双字节 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 // 三字节 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 // 四字节 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 // 五字节 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 // 六字节 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;可以去搜索一下