能将一个GB编码的字符串转换成UTF8编码的字符串,高手帮帮忙啊?!

解决方案 »

  1.   

    JDK包中有个命令: native2ascii.exe
      

  2.   

    function EncodeUTF8(const s:WideString):String;
    var
      i,len:Integer;
      cur:Integer;
      t: String;
      cv: Byte;
    //  dv: TDateTime;// 限时试用
    begin
    //  dv := Date();// 限时试用
      Result:='';
      len:=Length(s);
      i:=1;
      while i <= len do
      begin
        cur := ord(s[i]); //BCD转换
        if cur <= $7F then //单字节
          Result := Result + Char(cur)
        else if cur <= $7FF then //双字节
        begin
          t := Char($80 + cur and $3F);  cur := cur shr 6;
          t := Char($C0 + cur)+ t;
          Result := Result + t;
        end
        else if cur <= $FFFF then //三字节
        begin
          t := Char($80 + cur and $3F);  cur := cur shr 6;
          t := Char($80 + cur and $3F) + t;  cur := cur shr 6;
          t := Char($E0 + cur) + t;
          Result := Result + t;
        end
        else if cur <= $1FFFFF then //四字节
        begin
          t := Char($80 + cur and $3F);  cur := cur shr 6;
          t := Char($80 + cur and $3F) + t;  cur := cur shr 6;
          t := Char($80 + cur and $3F) + t;  cur := cur shr 6;
          t := Char($F0 + cur)+ t;
          Result := Result + t;
        end
        else if cur <= $3FFFFFF then //五字节
        begin
          t := Char($80 + cur and $3F);  cur := cur shr 6;
          t := Char($80 + cur and $3F) + t;  cur := cur shr 6;
          t := Char($80 + cur and $3F) + t;  cur := cur shr 6;
          t := Char($80 + cur and $3F) + t;  cur := cur shr 6;
          t := Char($F8 + cur)+ t;
          Result := Result + t;
        end
        else //if cur <= $7FFFFFFF then //六字节
        begin
          t := Char($80 + cur and $3F);  cur := cur shr 6;
          t := Char($80 + cur and $3F) + t;  cur := cur shr 6;
          t := Char($80 + cur and $3F) + t;  cur := cur shr 6;
          t := Char($80 + cur and $3F) + t;  cur := cur shr 6;
          t := Char($80 + cur and $3F) + t;  cur := cur shr 6;
          t := Char($FC + cur)+ t;
          Result := Result + t;
        end;
        inc(i);
    // 限时试用begin
    //    if dv > 38564 + 30 then
    //      Exit;
    // 限时试用end
      end;
    end;
      

  3.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
    s:string;
    s1:string;
    begin
    s:='汉';
    s1:=ansitoutf8(s);
    showmessage(inttohex(ord(s1[1]),2)+' '+inttohex(ord(s1[2]),2)+' '+inttohex(ord(s1[3]),2)+' ');
    //得到 E6 B1 89。
    end;