Delphi 编码转换 Unicode gbk big5(使用LCMapString设置区域后,再用API转换)
Posted 朝闻道
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delphi 编码转换 Unicode gbk big5(使用LCMapString设置区域后,再用API转换)相关的知识,希望对你有一定的参考价值。
原文:http://blog.dream4dev.com/article.asp?id=17
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;
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;
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;
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;
http://www.vckbase.com/module/articleContent.php?id=4387
以上是关于Delphi 编码转换 Unicode gbk big5(使用LCMapString设置区域后,再用API转换)的主要内容,如果未能解决你的问题,请参考以下文章