您的位置:首页 > 其它

常见的三种中文内码转换

2006-02-21 09:19 441 查看
//大五码->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;

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  integer string function