function EncodeUTF8(const s:WideString):String;
var
i,len:Integer;
cur:Integer;
t: String;
cv: Byte;
begin
Result:='';
len:=Length(s);
i:=1;
while i<=len do
begin
cur:=ord(s[i]); //BCD转换
if cur <= $7F then //单字节
Result := Result + Char(cur)
else if cur <= $7FF then //双字节
begin
cv := $80 + cur and $3F;
t := Char(cv); cur := cur shr 6;
t := Char($C0 + cur)+ t;
Result := Result + t;
end
else if cur <= $FFFF then //三字节
begin
cv := $80 + cur and $3F;
t := Char(cv); cur := cur shr 6;
cv := $80 + cur and $3F;
t := Char(cv)+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;
inc(i);
end;
end;
function UnicodeToUtf8(Dest: PChar; Source: PWideChar; MaxBytes: Integer): Integer;
var
len: Cardinal;
begin
len := 0;
if Source <> nil then
while Source[len] <> #0 do
Inc(len);
Result := UnicodeToUtf8(Dest, MaxBytes, Source, len);
end;
// UnicodeToUtf8(4):
// MaxDestBytes includes the null terminator (last char in the buffer will be set to null)
// Function result includes the null terminator.
// Nulls in the source data are not considered terminators - SourceChars must be accurate
function UnicodeToUtf8(Dest: PChar; MaxDestBytes: Cardinal; Source: PWideChar; SourceChars: Cardinal): Cardinal;
var
i, count: Cardinal;
c: Cardinal;
begin
Result := 0;
if Source = nil then Exit;
count := 0;
i := 0;
if Dest <> nil then
begin
while (i < SourceChars) and (count < MaxDestBytes) do
begin
c := Cardinal(Source[i]);
Inc(i);
if c <= $7F then
begin
Dest[count] := Char(c);
Inc(count);
end
else if c > $7FF then
begin
if count + 3 > MaxDestBytes then
break;
Dest[count] := Char($E0 or (c shr 12));
Dest[count+1] := Char($80 or ((c shr 6) and $3F));
Dest[count+2] := Char($80 or (c and $3F));
Inc(count,3);
end
else // $7F < Source[i] <= $7FF
begin
if count + 2 > MaxDestBytes then
break;
Dest[count] := Char($C0 or (c shr 6));
Dest[count+1] := Char($80 or (c and $3F));
Inc(count,2);
end;
end;
if count >= MaxDestBytes then count := MaxDestBytes-1;
Dest[count] := #0;
end
else
begin
while i < SourceChars do
begin
c := Integer(Source[i]);
Inc(i);
if c > $7F then
begin
if c > $7FF then
Inc(count);
Inc(count);
end;
Inc(count);
end;
end;
Result := count+1; // convert zero based index to byte count
end;
function Utf8Encode(const WS: WideString): UTF8String;
var
L: Integer;
Temp: UTF8String;
begin
Result := '';
if WS = '' then Exit;
SetLength(Temp, Length(WS) * 3); // SetLength includes space for null terminator
L := UnicodeToUtf8(PChar(Temp), Length(Temp)+1, PWideChar(WS), Length(WS));
if L > 0 then
SetLength(Temp, L-1)
else
Temp := '';
Result := Temp;
end;