本人近期在做多语言包,脚本文件里有很多汉字,我想在读取脚本文件txt时,直接转换成unicode,然后在程序里调用调用处理,哪位高手提供下参考资料,谢谢啦

解决方案 »

  1.   

    Delphi: 将汉字转换为Unicode码
    使用api函数中MultiByteToWidechar function UnicodeEncode(Str:string;CodePage:integer):WideString; 
    var 
      Len:integer; 
    begin 
      Len:=Length(Str)+1;  
      SetLength(Result,Len); 
      Len:=MultiByteToWideChar(CodePage,0,PChar(Str),-1,PWideChar(Result),Len);
      SetLength(Result,Len-1); //end is #0 
    end; function UnicodeDecode(Str:WideString;CodePage:integer):string; 
    var 
      Len:integer; 
    begin 
      Len:=Length(Str)*2+1;  //one for #0 
      SetLength(Result,Len); 
      Len:=WideCharToMultiByte(CodePage,0,PWideChar(Str),-1,PChar(Result),Len,nil,nil); 
      SetLength(Result,Len-1); 
    end; function Gb2Big5(Str:string):string; 
    begin 
      SetLength(Result,Length(Str)); 
      LCMapString(GetUserDefaultLCID,LCMAP_TRADITIONAL_CHINESE, 
      PChar(Str),Length(Str), 
      PChar(Result),Length(Result)); 
      Result:=UnicodeDecode(UnicodeEncode(Result,936),950); 
    end; function Big52Gb(Str:string):string; 
    begin 
      Str:=UnicodeDecode(UnicodeEncode(Str,950),936); 
      SetLength(Result,Length(Str)); 
      LCMapString(GetUserDefaultLCID,LCMAP_SIMPLIFIED_CHINESE,
        PChar(Str),Length(Str), 
       PChar(Result),Length(Result)); 
    end;function Utf8Encode(const WS: WideString): UTF8String; 
    var 
      L: Integer; 
      Temp: UTF8String; 
    begin 
      Result := ''; 
      if WS = '' then Exit; 
      SetLength(Temp, Length(WS) * 3); // SetLength includes space for null terminator 
      L := UnicodeToUtf8(PChar(Temp), Length(Temp)+1, PWideChar(WS), Length(WS)); 
      if L > 0 then 
        SetLength(Temp, L-1) 
      else 
        Temp := ''; 
      Result := Temp; 
    end; 
      

  2.   

    测试转的正确与否的话,可以先往文本文件中写入0xfffe,然后再依次写入Unicode编码,用记事本打开就可以知道字符是不是一样了。
      

  3.   

    function Tmainform.AnsiToUnicode(Ansi: string):string;
    var
      s:string;
      i:integer;
      j,k:string[2];
      a:array [1..1000] of char;
    begin
      s:='';
      StringToWideChar(Ansi,@(a[1]),500);
      i:=1;
      while ((a[i]<>#0) or (a[i+1]<>#0)) do
      begin
        j:=IntToHex(Integer(a[i]),2);
        k:=IntToHex(Integer(a[i+1]),2);
        s:=s+k+j;
        i:=i+2;
      end;
      Result:=s;
    end;function Tmainform.UnicodeToAnsi(Unicode: string):string;
    var
      s:string;
      i:integer;
      j,k:string[2]; function ReadHex(AString:string):integer;
     begin
      Result:=StrToInt('$'+AString);
     end;
    begin
      i:=1;
      s:='';
      while i<Length(Unicode)+1 do
      begin
        j:=Copy(Unicode,i+2,2);
        k:=Copy(Unicode,i,2);
        i:=i+4;
        s:=s+Char(ReadHex(j))+Char(ReadHex(k));
      end;
      if s<>'' then
        s:=WideCharToString(PWideChar(s+#0#0#0#0))
      else
        s:='';
      Result:=s;
    end;//自己搞定了
      

  4.   

    用Encoding.GetString方法去读取任意编码文件,再保存成unicode,很容易的。
      

  5.   

    既然用unicode了,存的时候就应该直接用utf-8,非要用gb2312不是折腾么