Utf8编码转换问题 Delphi
- 提问者网友:不要迷恋哥
- 2021-04-27 06:13
要求:给出Delphi7的具体代码...
- 五星知识达人网友:孤老序
- 2021-04-27 06:49
//将UTF-8编码转为ANSI编码
function UTF8ToAnsiString(utf8str:string; CodePage: integer):AnsiString;
var
i:integer;
buffer:widestring;
ch,c1,c2:byte;
begin
result:='';
i:=1;
while i<=Length(utf8str) do
begin
ch:=byte(utf8str[i]);
setlength(buffer,length(buffer)+1);
if (ch and $80)=0 then //1-byte
buffer[length(buffer)]:=widechar(ch)
else begin
if (ch AND $E0) = $C0 then
begin // 2-byte
inc(i);
c1 := byte(utf8str[i]);
buffer[length(buffer)]:=widechar((word(ch AND $1F) SHL 6) OR (c1 AND $3F));
end
else
begin // 3-byte
inc(i);
c1 := byte(utf8str[i]);
inc(i);
c2 := byte(utf8str[i]);
buffer[length(buffer)]:=widechar((word(ch AND $0F) SHL 12) OR (word(c1 AND $3F) SHL 6) OR (c2 AND $3F));
end;
end;
inc(i);
end; //while
i := WideCharToMultiByte(codePage,WC_COMPOSITECHECK or WC_DISCARDNS or WC_SEPCHARS or WC_DEFAULTCHAR,@buffer[1], -1, nil, 0, nil, nil);
if i>1 then
begin
SetLength(Result, i-1);
WideCharToMultiByte(codePage,WC_COMPOSITECHECK or WC_DISCARDNS or WC_SEPCHARS or WC_DEFAULTCHAR,@buffer[1], -1, @Result[1], i-1, nil, nil);
end;
end;
这是我百度博客上的技术文,你可以去看一下,有问题可以去那里留言……
http://hi.baidu.com/ray108/blog/item/be69e4423c9df3189313c6a8.html
- 1楼网友:蕴藏春秋
- 2021-04-27 08:26
用过滤器啦