请问字符怎么转UT8呀。。。
我在网上拷的代码试出来都是乱码,郁闷中....
我是这样写的
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;
procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(EncodeUTF8('中国'));
end;
结果是:涓浗
求解?