常见的三种中文内码转换

来源:互联网 发布:电脑打码软件 编辑:程序博客网 时间:2024/04/30 07:48

//五码->GBK

function BIG52GBK(Value: string): string;

var

  iLength, iReturn: Integer;

  sTmp: PWideChar;

  sDest: PChar;

  DefChar: Char;

begin

  Result := '';

  if Value = '' then exit;

  iLength := MultiByteToWideChar(950, 0, PChar(Value), -1, nil, 0);

  GetMem(sTmp, (iLength+1)*SizeOf(WideChar));

  ZeroMemory(sTmp, (iLength+1)*SizeOf(WideChar));

 

  //950 ANSI/OEM - Traditional Chinese (Taiwan; Hong Kong SAR, PRC)

  MultiByteToWideChar(950, 0, PChar(Value), Length(Value),

    sTmp, iLength+1);

 

  DefChar := '?';

  iReturn := WideCharToMultiByte(936, 0, sTmp, -1, nil, 0, @DefChar, nil);

  GetMem(sDest, iReturn);

  ZeroMemory(sDest, iReturn);

 

  WideCharToMultiByte(936, 0, sTmp, -1, sDest, iReturn, @DefChar, nil);

 

  Result := StrPas(sDest);

  FreeMem(sTmp);

  FreeMem(sDest);

end;

 

//GBK->五码

function GBK2BIG5(Value: string): string;

var

  iLength, iReturn: Integer;

  sTmp: PWideChar;

  sDest: PChar;

  DefChar: Char;

begin

  Result := '';

  if Value = '' then exit;

  iLength := MultiByteToWideChar(936, 0, PChar(Value), -1, nil, 0);

  GetMem(sTmp, (iLength+1)*SizeOf(WideChar));

  ZeroMemory(sTmp, (iLength+1)*SizeOf(WideChar));

 

  MultiByteToWideChar(936, 0, PChar(Value), Length(Value),

    sTmp, iLength+1);

 

  DefChar := '?';

  iReturn := WideCharToMultiByte(950, 0, sTmp, -1, nil, 0, @DefChar, nil);

  GetMem(sDest, iReturn);

  ZeroMemory(sDest, iReturn);

 

  WideCharToMultiByte(950, 0, sTmp, -1, sDest, iReturn, @DefChar, nil);

 

  Result := StrPas(sDest);

  FreeMem(sTmp);

  FreeMem(sDest);

end;

 

function MAKELANGID(usPrimaryLanguage, usSubLanguage: WORD): WORD;

begin

 Result := (usSubLanguage shl 10) or usPrimaryLanguage;

end;

 

function MAKELCID(wLanguageID: WORD; wSortID: WORD = SORT_DEFAULT): LCID;

begin

 Result := MakeLong(wLanguageID, wSortID);

end;

 

//GB2312->GBK

function GB2GBK(Value: string): string;

var

  iLength: Integer;

  sTmp: PChar;

begin

  iLength := Length(Value);

  GetMem(sTmp, iLength+1);

 

  LCMapString(MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),

    SORT_CHINESE_PRC),

    LCMAP_TRADITIONAL_CHINESE, PChar(Value),

    iLength+1, sTmp, iLength+1);

  Result :=StrPas(sTmp);

 

  FreeMem(sTmp);

end;

 

//GBK->GB2312

function GBK2GB(Value: string): string;

var

  iLength: Integer;

  sTmp: PChar;

begin

  iLength := Length(Value);

  GetMem(sTmp, iLength+1);

 

  LCMapString(MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),

    SORT_CHINESE_BIG5),

    LCMAP_SIMPLIFIED_CHINESE, PChar(Value),

    iLength+1, sTmp, iLength+1);

  Result :=StrPas(sTmp);

 

  FreeMem(sTmp);

end;