首页  编辑  

GB<-->BIG

Tags: /超级猛料/Format.格式,单位/Encode.编码、格式/   Date Created:
昨天摔了一跤 <windoze@yeah.net>
在Win2K下自己写很容易,下面给你几个函数,函数名里有String的是AnsiString版,
没有的是PChar版
uses
    SysUtils,
    Windows;

function StrConv(pszSrc: PChar; cpSrc: UINT; pszDest: PChar; cbDest: Longint; cpDest: UINT): Longint;
var
    buf: PWideChar;
    cwLen: Longint;
begin
    cwLen := MultiByteToWideChar(cpSrc, 0, pszSrc, -1, nil, 0);
    GetMem(buf, (cwLen + 1) * SizeOf(WideChar));
    ZeroMemory(buf, (cwLen + 1) * SizeOf(WideChar));
    MultiByteToWideChar(cpSrc, 0, pszSrc, StrLen(pszSrc), buf, cwLen + 1);
    Result := WideCharToMultiByte(cpDest, 0, buf, -1, pszDest, cbDest, nil, nil);
    FreeMem(buf);
end;

function StringConv(const Src: string; cpSrc, cpDest: UINT): string;
var
    buf: PChar;
    len: Longint;
begin
    len := StrConv(PChar(Src), cpSrc, nil, 0, cpDest);
    GetMem(buf, len);
    ZeroMemory(buf, len);
    StrConv(PChar(Src), cpSrc, buf, len, cpDest);
    Result := string(buf);
    FreeMem(buf);
end;

function CHTStringToCHSString(Src: string): string;
var
    buf: PChar;
    len: Integer;
begin
    len := LCMapString($804, // LCID_CHINESE_SIMPLIFIED
        LCMAP_SIMPLIFIED_CHINESE, PChar(Src), -1, nil, 0);
    GetMem(buf, len);
    ZeroMemory(buf, len);
    LCMapString($804, // LCID_CHINESE_SIMPLIFIED
        LCMAP_SIMPLIFIED_CHINESE, PChar(Src), -1, buf, len);
    Result := string(buf);
    FreeMem(buf);
end;

function CHSStringToCHTString(Src: string): string;
var
    buf: PChar;
    len: Integer;
begin
    len := LCMapString($404, // LCID_CHINESE_SIMPLIFIED
        LCMAP_TRADITIONAL_CHINESE, PChar(Src), -1, nil, 0);
    GetMem(buf, len);
    ZeroMemory(buf, len);
    LCMapString($404, // LCID_CHINESE_SIMPLIFIED
        LCMAP_TRADITIONAL_CHINESE, PChar(Src), -1, buf, len);
    Result := string(buf);
    FreeMem(buf);
end;

function CHTToCHS(pszSrc, pszDest: PChar; cbDest: Longint): Longint;
begin
    Result := LCMapString($804, // LCID_CHINESE_SIMPLIFIED
        LCMAP_SIMPLIFIED_CHINESE, pszSrc, -1, pszDest, cbDest);
end;

function CHSToCHT(pszSrc, pszDest: PChar; cbDest: Longint): Longint;
begin
    Result := LCMapString($404, // LCID_CHINESE_SIMPLIFIED
        LCMAP_TRADITIONAL_CHINESE, pszSrc, -1, pszDest, cbDest);
end;

function Big5StringToGBString(Src: string): string;
begin
    Result := CHTStringToCHSString(StringConv(Src, 950, 936));
end;

function Big5ToGB(pszSrc, pszDest: PChar; cbDest: Longint): Longint;
var
    buf: PChar;
    len: Integer;
begin
    len := StrLen(pszSrc) + 1;
    GetMem(buf, len * 2);
    ZeroMemory(buf, len * 2);
    StrConv(pszSrc, 950, buf, 936, len * 2);
    Result := CHTToCHS(buf, pszDest, cbDest);
    FreeMem(buf);
end;

function GBStringToBig5String(Src: string): string;
begin
    Result := CHSStringToCHTString(StringConv(Src, 936, 950));
end;

function GBToBig5(pszSrc, pszDest: PChar; cbDest: Longint): Longint;
var
    buf: PChar;
    len: Integer;
begin
    len := StrLen(pszSrc) + 1;
    GetMem(buf, len * 2);
    ZeroMemory(buf, len * 2);
    StrConv(pszSrc, 936, buf, 950, len * 2);
    Result := CHSToCHT(buf, pszDest, cbDest);
    FreeMem(buf);
end;

Gb2Big5.pas (617.5KB)