把unicode编码转成GB2312呢又怎么做?

解决方案 »

  1.   

    试试这个:
    {*******************************************************}
    {                                                       }
    {       Author: 王运龙                                  }
    {                                                       }
    {  [email protected]                                      }
    {                                                       }
    {*******************************************************}
    unit wnCCVMain;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        btnConvert: TButton;
        rbtnInputHex: TRadioButton;
        rbtnInputChara: TRadioButton;
        memInput: TMemo;
        panResult: TPanel;
        rbtnUToA: TRadioButton;
        rbtnAToU: TRadioButton;
        Bevel1: TBevel;
        gbxUnicode: TGroupBox;
        memUnicode: TMemo;
        gbxAscii: TGroupBox;
        memAscii: TMemo;
        procedure btnConvertClick(Sender: TObject);
        procedure rbtnInputCharaClick(Sender: TObject);
        procedure rbtnInputHexClick(Sender: TObject);
      private
        { Private declarations }    function GetHex(const aStr: string): string;
        function GetChars(aHexStr: string): string;
        //将UnicodeHex AscII字串转换为ANSI Ascii
        function UnicodeHexToStr(const asUnicodeHex: string): string;
        function ChinaToUnicode(const aWideStr: WideString): string;
        function UnicodeHex(const aWideStr: WideString): string;
        function FormatHexDisp(const asHex: string): string;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}function TForm1.UnicodeHexToStr(const asUnicodeHex: string): string;
    var
      i: Integer;
      sTemp: string;
    begin
      //“中国网管程序:操作说明”的Unicode编码16进制为:
      //4E2D 56FD 7F51 7BA1 7A0B 5E8F FF1A 64CD 4F5C 8BF4 660E
      for i := 1 to Length(asUnicodeHex) do
      begin
        if i mod 4 = 0 then
        begin
          sTemp := Copy(asUnicodeHex, i - 3, 4);
          sTemp := WideChar(StrToIntDef('$' + sTemp, 0));
          Result := Result + sTemp;
        end;
      end;
    end;
    function TForm1.ChinaToUnicode(const aWideStr: WideString): string;
    var
      sUnicodeHex: string;
      i : integer;
    begin
      for i := 1 to Length(aWideStr) do
      begin
        sUnicodeHex := Format('%.4x', [Word(aWideStr[i])]);
        sUnicodeHex := Chr(StrToInt('$' + Copy(sUnicodeHex, 3, 2))) +
            Chr(StrToInt('$' + copy(sUnicodeHex, 1, 2)));
        Result := Result + sUnicodeHex;
      end;
    end;function TForm1.UnicodeHex(const aWideStr: WideString): string;
    var
      i: Integer;
    begin
      for i := 1 to length(aWideStr) do
      begin
        Result := Result + Format('%.4x', [Word(aWideStr[i])]);
      end;
    end;function TForm1.FormatHexDisp(const asHex: string): string;
    var
      i, iLen: Integer;
    begin
      Result := asHex;
      iLen := Length(Result);
      if Odd(iLen) then
      begin
        Result := '0' + Result;
        Inc(iLen);
      end;  for i := iLen downto 1 do
      begin
        if Odd(i) then Continue;
        
        Insert(' ', Result, i - 1);
      end;
      Result := Trim(Result);
    end;procedure TForm1.btnConvertClick(Sender: TObject);
    var
      s: string;
    begin
      s := memInput.Text;
      if rbtnInputChara.Checked then
      begin
        memUnicode.Text := FormatHexDisp(UnicodeHex(s));
        memAscii.Text := FormatHexDisp(GetHex(s));
      end else
      begin
        s := StringReplace(s, ' ', '', [rfReplaceAll, rfIgnoreCase]);
        if rbtnUToA.Checked then
        begin
          memUnicode.Text := UnicodeHexToStr(s);
          memAscii.Text := FormatHexDisp(GetHex(memUnicode.Text));
        end else
        begin
          memUnicode.Text := GetChars(s);
          memAscii.Text := UnicodeHex(memUnicode.Text);
        end;
      end;
    end;procedure TForm1.rbtnInputCharaClick(Sender: TObject);
    begin
      memUnicode.Clear;
      memAscii.Clear;
      gbxUnicode.Caption := 'Unicode编码';
      gbxAscii.Caption := 'Ascii编码';
      rbtnUToA.Enabled := False;
      rbtnAToU.Enabled := False;
    end;procedure TForm1.rbtnInputHexClick(Sender: TObject);
    begin
      memUnicode.Clear;
      memAscii.Clear;  gbxUnicode.Caption := '转换后的文本';
      gbxAscii.Caption := 'Hex编码';
      rbtnUToA.Enabled := True;
      rbtnAToU.Enabled := True;
    end;function TForm1.GetHex(const aStr: string): string;
    var
      i: Integer;
    begin
      for i := 1 to Length(aStr) do
      begin
        Result := Result + Format('%.2x', [Ord(aStr[i])]);
      end;
    end;function TForm1.GetChars(aHexStr: string): string;
    var
      i: Integer;
    begin
      aHexStr := StringReplace(aHexStr, ' ', '', [rfReplaceAll, rfIgnoreCase]);
      for i := 1 to Length(aHexStr) do
      begin
        if Odd(i) then
        begin
          Result := Result + Char(StrToIntDef('$' + Copy(aHexStr, i, 2), 0));
        end;
      end;
    end;end.
      

  2.   

    通过Delphi的WideString类型转换,可以巧妙地实现GB2312到Unicode的编码转换(注意代码页和操作系统相关联)。下面是实现中文编码的部分Delphi 5代码: 
    // 中文格式编码,s为Unicode String 
    function Encode2(var s:WideString):String; 
    var 
    i,len:Integer; 
    cur:Integer; 
    t:String; 
    begin 
    Result:=‘’; 
    len:=Length(s); 
    i:=1; 
    while i<=len do 
    begin 
    cur:=ord(s[i]); 
    //BCD转换 
    FmtStr(t,‘%4.4X’,[cur]); 
    Result:=Result+t; 
    inc(i); 
    end; 
    end; 
      

  3.   

    還有個做法: http://delphi.ktop.com.tw/topic.asp?TOPIC_ID=328951, 2. 可以,把 MultiByteToWideChar 的第一個參數傳入 936,就可以把 GB 碼轉成Unicode3. 如果只考慮轉碼,不考慮繁簡寫法不同的話,可以用下列程式function Big5ToGB(sBig5: string): string;
    var ws: array[0..32767] of WideChar;
        s: array[0..65535] of char;
    begin
      ZeroMemory(@ws, Length(ws) * SizeOf(WideChar));
      ZeroMemory(@s, Length(s) * SizeOf(char));
      MultiByteToWideChar(950, MB_COMPOSITE, PChar(sBig5), -1, @ws, Length(ws));
      WideCharToMultiByte(936, WC_COMPOSITECHECK, @ws, -1, @s, Length(s), '?', PBOOL(false));
      result := s;
    end;
    若要達到轉換後轉繁簡寫法不同的話,以上程式不適用
    4. WideCharToMultiByte,第一個參數傳入 936有關 code page 請看 Windows SDK Help 中 IsValidCodePage 的說明简体操作系统下,
    对GB2312,把 MultiByteToWideChar 的第一個參數傳入 936,得到的不是UNICODE,而是GBK的简体
    同样,对Big5,传入950,得到的也是GBK的繁体,不是UNICODE,
    只有把结果再用UTF8Encode函数,转一下,才得到UNICODE(UTF8),
    你把结果放到一个htm文件,在IE里试试就知道了,套用不同的编码,看到不一样的乱码在简体系统,转成GBK的繁体,CharSet用Default就能显示“繁体”了,但由于这个“繁体”是BGK的“繁体”,拿到繁体操作系统(Big5)下,一样会是乱码