将utf-8字符串转为代码页为codepage的ansistring,不知对你有用否:
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;
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;
程序需要的GB2312.txt文件 gb2utf8.php
<? //Program writen by sadly www.phpx.com function gb2utf8($gb)
{
if(!trim($gb))
return $gb;
$filename="gb2312.txt";
$tmp=file($filename);
$codetable=array();
while(list($key,$value)=each($tmp))
$codetable[hexdec(substr($value,0,6))]=substr($value,7,6); $utf8="";
while($gb)
{
if (ord(substr($gb,0,1))>127)
{
$this=substr($gb,0,2);
$gb=substr($gb,2,strlen($gb));
$utf8.=u2utf8(hexdec($codetable[hexdec(bin2hex($this))-0x8080]));
}
else
{
$gb=substr($gb,1,strlen($gb));
$utf8.=u2utf8(substr($gb,0,1));
}
} $ret="";
for($i=0;$i<strlen($utf8);$i+=3)
$ret.=chr(substr($utf8,$i,3)); return $ret;
} function u2utf8($c)
{
for($i=0;$i<count($c);$i++)
$str="";
if ($c < 0x80) {
$str.=$c;
}
else if ($c < 0x800) {
$str.=(0xC0 | $c>>6);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x10000) {
$str.=(0xE0 | $c>>12);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x200000) {
$str.=(0xF0 | $c>>18);
$str.=(0x80 | $c>>12 & 0x3F);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
return $str;
}
?> 调用举例:通过GD输出 "中国" 两个汉字
example.php <?
//Header("Content-type: image/gif");
$im = imagecreate(400,300);
$bkg = ImageColorAllocate($im, 0,0,0);
$clr = ImageColorAllocate($im, 255,255,255);
$fnt = "d:/winnt/fonts/simhei.ttf";
include("gb2utf8.php");
$str = gb2utf8("中国");
ImageTTFText($im, 20, 0, 10, 20, $clr, $fnt, $str);
ImageGif($im);
ImageDestroy($im);
?>
function AnsiToUnicode(Ansi: string):string;
var
s:string;
i:integer;
j,k:string[2];
a:array [1..1000] of char;
begin
s:='';
StringToWideChar(Ansi,@(a[1]),500);
i:=1;
while ((a[i]<>#0) or (a[i+1]<>#0)) do begin
j:=IntToHex(Integer(a[i]),2);
k:=IntToHex(Integer(a[i+1]),2);
s:=s+k+j;
i:=i+2;
end;
Result:=s;
end;function ReadHex(AString:string):integer;
begin
Result:=StrToInt('$'+AString)
end;function UnicodeToAnsi(Unicode: string):string;
var
s:string;
i:integer;
j,k:string[2];
begin
i:=1;
s:='';
while i<Length(Unicode)+1 do begin
j:=Copy(Unicode,i+2,2);
k:=Copy(Unicode,i,2);
i:=i+4;
s:=s+Char(ReadHex(j))+Char(ReadHex(k));
end;
if s<>'' then
s:=WideCharToString(PWideChar(s+#0#0#0#0))
else
s:='';
Result:=s;
end;function ToGBK(Str:string):string;
begin
result := inttohex(byte(Str[1])*$100+byte(Str[2]),2);
end;