unit CodeCn;<br><br>interface<br><br>uses<br> Windows;<br><br>const<br> CHINESE_SIMPLIFIED = 0;<br> CHINESE_TRADITIONAL = 1;<br><br>function Big52GB(const Source: string): string;<br>function Big52GBT(const Source: string): string;<br>function GB2Big5(const Source: string): string;<br>function GB2GBT(const Source: string): string;<br>function GBT2GB(const Source: string): string;<br><br>implementation<br><br>function MAKELANGID(usPrimaryLanguage, usSubLanguage: WORD): WORD;<br>begin<br> Result := (usSubLanguage shl 10) or usPrimaryLanguage;<br>end;<br><br>function MAKELCID(wLanguageID: WORD; wSortID: WORD = SORT_DEFAULT): LCID;<br>begin<br> Result := MakeLong(wLanguageID, wSortID);<br>end;<br><br>function ToUnicode(const Source: string; nEncoding: Integer): WideString;<br>var<br> nLength: Integer;<br> nLanguage: Integer;<br>begin<br> if nEncoding = CHINESE_SIMPLIFIED then<br> nLanguage := 936<br> else if nEncoding = CHINESE_TRADITIONAL then<br> nLanguage := 950<br> else<br> nLanguage := CP_ACP;<br><br> nLength := MultiByteToWideChar(nLanguage, 0, PChar(Source), Length(Source), nil, 0);<br> SetLength(Result, nLength);<br> MultiByteToWideChar(nLanguage, 0, PChar(Source), Length(Source), PWideChar(Result), nLength);<br>end;<br><br>function ToMultiByte(const Source: WideString; nEncoding: Integer): string;<br>var<br> nLength: Integer;<br> nLanguage: Integer;<br>begin<br> if nEncoding = CHINESE_SIMPLIFIED then<br> nLanguage := 936<br> else if nEncoding = CHINESE_TRADITIONAL then<br> nLanguage := 950<br> else<br> nLanguage := CP_ACP;<br><br> nLength := WideCharToMultiByte(nLanguage, 0, PWideChar(Source), Length(Source), nil, 0, nil, nil);<br><br> SetLength(Result, nLength);<br> WideCharToMultiByte(nLanguage, 0, PWideChar(Source), Length(Source), PChar(Result), nLength, nil, nil);<br>end;<br><br>function Big52GB(const Source: string): string;<br>begin<br> Result := Big52GBT(Source);<br> Result := GBT2GB(Result);<br>end;<br><br>function Big52GBT(const Source: string): string;<br>var<br> nLength: Integer;<br> WS: WideString;<br> nResultLength: Integer;<br>begin<br> nLength := MultiByteToWideChar(950, 0, PChar(Source), Length(Source), nil, 0);<br> SetLength(WS, nLength);<br> MultiByteToWideChar(950, 0, PChar(Source), Length(Source), PWideChar(WS), nLength);<br><br> nResultLength := WideCharToMultiByte(936, 0, PWideChar(WS), nLength, nil, 0, nil, nil);<br> SetLength(Result, nResultLength);<br> WideCharToMultiByte(936, 0, PWideChar(WS), nLength, PChar(Result), nResultLength, nil, nil);<br>end;<br><br>function GBT2GB(const Source: string): string;<br>var<br> dwLocale: LCID;<br> wLangID: Word;<br> nLength: Integer;<br>begin<br> wLangID := MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED);<br> dwLocale := MAKELCID(wLangID, SORT_CHINESE_PRC);<br> nLength := LCMapString(dwLocale, LCMAP_SIMPLIFIED_CHINESE, PChar(Source), Length(Source), nil, 0);<br> SetLength(Result, nLength);<br> LCMapString(dwLocale, LCMAP_SIMPLIFIED_CHINESE, PChar(Source), Length(Source), PChar(Result), nLength);<br>end;<br><br>function GB2Big5(const Source: string): string;<br>var<br> S: string;<br> nLength: Integer;<br> WS: WideString;<br> nResultLength: Integer;<br>begin<br> S := GB2GBT(Source);<br> nLength := MultiByteToWideChar(936, 0, PChar(S), Length(S), nil, 0);<br> SetLength(WS, nLength);<br> MultiByteToWideChar(936, 0, PChar(S), Length(S), PWideChar(WS), nLength);<br><br> nResultLength := WideCharToMultiByte(950, 0, PWideChar(WS), nLength, nil, 0, nil, nil);<br> SetLength(Result, nResultLength);<br> WideCharToMultiByte(950, 0, PWideChar(WS), nLength, PChar(Result), nResultLength, nil, nil);<br>end;<br><br>//将GB简体转换到GBT繁体<br>function GB2GBT(const Source: string): string;<br>var<br> dwLocale: LCID;<br> wLangID: Word;<br> nLength: Integer;<br>begin<br> wLangID := MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED);<br> dwLocale := MAKELCID(wLangID, SORT_CHINESE_PRC);<br> nLength := LCMapString(dwLocale, LCMAP_TRADITIONAL_CHINESE, PChar(Source), Length(Source), nil, 0);<br> SetLength(Result, nLength);<br> LCMapString(dwLocale, LCMAP_TRADITIONAL_CHINESE, PChar(Source), Length(Source), PChar(Result), nLength);<br>end;<br><br>end.<br>