unicode 编码与解码
delphi 编码转换 unicode gbk big5
(转载)
[ 2006-03-23 15:39:23 | 作者: Admin ]
字体大小: 大 | 中 | 小
以下代码在DELPHI 7上调试通过,主要使用了api函数中MultiByteToWidechar
function UnicodeEncode(Str:string;CodePage:integer):WideString;
var
Len:integer;
begin
Len:=Length(Str)+1;
SetLength(Result,Len);
Len:=MultiByteToWideChar(CodePage,0,PChar(Str),-1,PWideChar(Result),Len);
SetLength(Result,Len-1); //end is #0
end;
function UnicodeDecode(Str:WideString;CodePage:integer):string;
var
Len:integer;
begin
Len:=Length(Str)*2+1; //one for #0
SetLength(Result,Len);
Len:=WideCharToMultiByte(CodePage,0,PWideChar(Str),-1,PChar(Result),Len,nil,nil);
SetLength(Result,Len-1);
end;
function Gb2Big5(Str:string):string;
begin
SetLength(Result,Length(Str));
LCMapString(GetUserDefaultLCID,LCMAP_TRADITIONAL_CHINESE,
PChar(Str),Length(Str),
PChar(Result),Length(Result));
Result:=UnicodeDecode(UnicodeEncode(Result,936),950);
end;
function Big52Gb(Str:string):string;
begin
Str:=UnicodeDecode(UnicodeEncode(Str,950),936);
SetLength(Result,Length(Str));
LCMapString(GetUserDefaultLCID,LCMAP_SIMPLIFIED_CHINESE,
PChar(Str),Length(Str),
PChar(Result),Length(Result));
end;
关键使用了UnicodeToUtf8这个函数
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; -
=======================
方法二:
后来想到了用Unicode编码,但该死的Delphi控件竟然不支持Unicode,后来在网上找到了Tnt控件,可以支持Unicode不过而项目已经差不多快好了,要大规模换控件是不可能的,就想到读一下源代码,看看Tnt控件是如何做到的。读完之后一阵绝望,Tnt控件几乎全用W结尾的API,连创建窗体都是用CreateWindowExW,那还有什么好话说呢,自己重做就不如全换Tnt控件
----------------------
Delphi: 将unicode码转换为汉字
Night @ 2004-12-01 21:12
function UnicodeToAnsi(SubUnicode: string):string; //将unicode码转换为汉字
var a:array[0..500] of char;
s1,s2:char;
substr1,substr2,s:string;
str:string;
i:integer;
begin
if length(SubUnicode) mod 4 = 0 then
Begin
str:='';
for i:=1 to length(SubUnicode) div 4 do
Begin
s:='';
substr1:=copy(SubUnicode,i*4-3,2);
substr2:=copy(SubUnicode,i*4-1,2);
s1:=chr(hextoint(substr1));
s2:=chr(hextoint(substr2));
s:=s+s2+s1;
strpcopy(a,s);
str:=str+copy(widechartostring(@(a[0])),1,2);
end;
result:=str;
end;
end;
function HexToInt(hex:string):cardinal;
const cHex='0123456789ABCDEF';
var mult,i,loop:integer;
begin
result:=0;
mult:=1;
for loop:=length(hex)downto 1 do
begin
i:=pos(hex[loop],cHex)-1;
if (i<0) then i:=0;
inc(result,(i*mult));
mult:=mult*16;
end;
end;
-----
tnt控件下载地址
tnt delphi unicode controls 版 本:
文件大小:314.7k
软件语言:简体中文
授权方式:免费版
相关链接:程序演示
开 发 商:本站搜集
运行环境:
添加时间:2006-01-13
发布人:本站搜集
本站搜集最近发布的资源:
留言通知我们。
*欢迎广大作者给我们提供源码以及使用说明。下载操作前请详细阅读[版权说明].
资源介绍:
完全解决了vcl控件不支持unicode的问题,让你开发国际化的程序变得更为高效,含源码。
---------------------------------------------------------------------------------------
我是这样做的。
function AnsiToUnicode(Ansi: string): string;
var
s:string;
i:integer;
j,k:string[2];
a:array [1..1000] of char;
begin
try
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;
except
Result:='';
end;
end;
function ReadHex(AString: string): integer;
begin
try
Result:=StrToInt('$'+AString);
except
Result:=0;
end;
end;
function UnicodeToAnsi(Unicode: string): string;
var
s:string;
i:integer;
j,k:string[2];
begin
i:=1;
s:='';
try
while i'' then
s:=WideCharToString(PWideChar(s+#0#0#0#0))
else
s:='';
Result:=s;
except
Result:='';
end;
end;
来源:http://www.delphibbs.com/keylife/iblog_show.asp?xid=24551