在delphi程序中如何把中文已UTF-8编码?急!!

Xiaowq 2004-07-07 02:01:07
多谢!
...全文
1026 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ahjoe 2005-07-30
  • 打赏
  • 举报
回复
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;

僵哥 2005-07-30
  • 打赏
  • 举报
回复
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;
icerainy 2005-07-30
  • 打赏
  • 举报
回复
没有?怎么回事?高手说说为什么没有,是版本的问题吗?
xiongrubin 2005-07-30
  • 打赏
  • 举报
回复
那是什么啊 我都不知道是什么用处呢
那位能解释清楚啊 谢谢
ahjoe 2005-07-30
  • 打赏
  • 举报
回复
我的DELPHI没有UTF8Encode
sdqhlyf 2005-05-25
  • 打赏
  • 举报
回复
楼上的对 !
masuz1 2005-01-17
  • 打赏
  • 举报
回复
uses
System;


String to UTF-8 Code:

function Utf8Encode(const WS: WideString): UTF8String;

UTF-8 to String:

function Utf8Decode(const S: UTF8String): WideString;
Xiaowq 2004-07-07
  • 打赏
  • 举报
回复
to sherry3000(怀念巴乔)
对呀!
sherry3000 2004-07-07
  • 打赏
  • 举报
回复
你到底想问什么?
是如何将中文编码成UTF-8吗?

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧