Convert encoding --- 解决乱码问题


【原因】

我自己用Sublime Text 2 编Matlab Mfile, 到Matlab Editor中,会出现乱码. 虽然可以通过更改Matlab Font解决问题,但是这样的字体不如原来好看(个人观点).

另外,如果是苹果电脑或者Linux中编的mfile(一般都是UTF-8),然而Windows上默认是ANSI(中文windows就是GBK,英文俄文等windows系统则是相应的其他encoding方式),这样在复制mfile到其他系统中时常常出现乱码. 可以用浏览器打开mfile,改变encoding,然后复制文字到新mfile----这种解决方法太麻烦.

我现在实现的方法,可在Total Commander的Start menu中添加命令, 快捷键一步解决encoding转换问题.

用iconv.exe实现转换,我自己用Autohotkey添加了GUI(图形化界面)入口,同时简化了命令行入口(特别是在TC中,快捷键直接解决).
GUI:可以双击->打开选择需转换文档, 也可以拖曳文档到程序上实现. 为了方便,可以创建一个快捷方式到Sendto文件夹(发送到),之后只要右击,点"发送到",选择这个快捷方式即可. 打开Sendto文件夹方法: 快捷键Win+R,输入"shell:sendto",回车即可.

点击下载

可自行在ConvertList.ini添加转化格式.
支持encoding格式如下:
European languages
    ASCII, ISO-8859-{1,2,3,4,5,7,9,10,13,14,15,16}, KOI8-R, KOI8-U, KOI8-RU, CP{1250,1251,1252,1253,1254,1257}, CP{850,866,1131}, Mac{Roman,CentralEurope,Iceland,Croatian,Romania}, Mac{Cyrillic,Ukraine,Greek,Turkish}, Macintosh
Semitic languages
    ISO-8859-{6,8}, CP{1255,1256}, CP862, Mac{Hebrew,Arabic}
Japanese
    EUC-JP, SHIFT_JIS, CP932, ISO-2022-JP, ISO-2022-JP-2, ISO-2022-JP-1
Chinese
    EUC-CN, HZ, GBK, CP936, GB18030, EUC-TW, BIG5, CP950, BIG5-HKSCS, BIG5-HKSCS:2004, BIG5-HKSCS:2001, BIG5-HKSCS:1999, ISO-2022-CN, ISO-2022-CN-EXT
Korean
    EUC-KR, CP949, ISO-2022-KR, JOHAB
Armenian
    ARMSCII-8
Georgian
    Georgian-Academy, Georgian-PS
Tajik
    KOI8-T
Kazakh
    PT154, RK1048
Thai
    ISO-8859-11, TIS-620, CP874, MacThai
Laotian
    MuleLao-1, CP1133
Vietnamese
    VISCII, TCVN, CP1258
Platform specifics
    HP-ROMAN8, NEXTSTEP
Full Unicode
    UTF-8
    UCS-2, UCS-2BE, UCS-2LE
    UCS-4, UCS-4BE, UCS-4LE
    UTF-16, UTF-16BE, UTF-16LE
    UTF-32, UTF-32BE, UTF-32LE
    UTF-7
    C99, JAVA 
Full Unicode, in terms of uint16_t or uint32_t (with machine dependent endianness and alignment)
    UCS-2-INTERNAL, UCS-4-INTERNAL

同时,修改prompt.txt中文字(之后版本会改进,让程序根据ConvertList.ini内容设置自动生成prompt.txt文件).
下载的原始prompt.txt,内容如下:
1. UTF-8	to	GBK
2. GBK		to	UTF-8
	
对应界面如下(修改prompt.txt内容后会相应改变:


【我的Autohotkey源代码如下】:
; ini file to txt file for GUI prompt
FileRead, txtContent, %A_ScriptDir%\argument\prompt.txt
Gui, Font, s18 c0000FF, Ariel
Gui, Add, Text,section, Converting List:
Gui, Font, s14 c000000, Ariel
Gui, Add, Text,section, %txtContent%
Gui, Font, s18 c0000FF, Ariel
Gui, Add, Text,section, Choose No:
Gui, Add, Edit, vModel ys w30
Gui, Add, Button,section xs h50 w100 default, &OK
Gui, Add, Button,xs+200 ys h50 w100 , &Cancel
Gui, Show, xcenter ycenter, Choose convertting encodings
return


ButtonOK:
Gui, Submit
;Msgbox,OK No.%Model%
c_left = %Model%Left
c_right = %Model%Right
;Msgbox,%c_left% to %c_right%
IniRead, c_left, %A_ScriptDir%\argument\ConvertList.ini, ConvertList, %c_left%
IniRead, c_right, %A_ScriptDir%\argument\ConvertList.ini, ConvertList, %c_right%
;Msgbox,%c_left% to %c_right%

If 0 =0
{
  ;Convert
  FileSelectFile, files, M3, , Open an Text file or files needed to convert endoing.
  Loop, parse, files, `n
  {
      if a_index = 1
          ;MsgBox, The selected files are all contained in %A_LoopField%. ;Dir
          dirInput=%A_LoopField%
      else
      {
        filename=%A_LoopField%
        pathInput=%dirInput%\%filename%
        SplitPath,pathInput,,,extInput,nameInputNoExt
        SetWorkingDir %dirInput%
        pathOutput=%nameInputNoExt%_ori.%extInput%
        Filecopy,%pathInput%,%pathOutput%
        pathTemp =%nameInputNoExt%_temp.%extInput%
        ;Convert encoding ->****.***
        App_iconv = %A_ScriptDir%\argument\iconv.exe -f %c_left% -t %c_right%
        command =%App_iconv% %nameInputNoExt%_ori.%extInput% > %pathTemp%
        SetWorkingDir %dirInput%
        ; Here this batch script is used to run command because that it doesn't work directly with "run, %command%"(Strange!)
        FileDelete, %nameInputNoExt%_command.bat
        If ErrorLevel
        {
        FileAppend, %command%,%nameInputNoExt%_command.bat
        runwait, %nameInputNoExt%_command.bat
        FileDelete, %nameInputNoExt%_command.bat
        FileDelete, %pathInput%
        Filecopy, %pathTemp%, %pathInput%
        FileDelete,%pathTemp%
        }
        else
        {
          Msgbox, Already converted
        }
    }
  }
}
else
{
  ;Convert
  Loop, %0%  ; For each parameter: %P%S in TC start menu
  {
      pathInput := %A_Index%  ; Fetch the contents of the variable whose path is contained in A_Index.
      ;original file -> ****_ori.***
      SplitPath,pathInput,,dirInput,extInput,nameInputNoExt
      SetWorkingDir, %dirInput%
    pathOutput=%nameInputNoExt%_ori.%extInput%
      Filecopy,%pathInput%,%pathOutput%
      pathTemp =%nameInputNoExt%_temp.%extInput%
      ;Convert encoding ->****.***
      App_iconv = %A_ScriptDir%\argument\iconv.exe -f %c_left% -t %c_right%
      command =%App_iconv% %nameInputNoExt%_ori.%extInput% > %pathTemp%
      ; Here this batch script is used to run command because that it doesn't work directly with "run, %command%"(Strange!)
      FileDelete, %nameInputNoExt%_command.bat
      If ErrorLevel
      {
      FileAppend, %command%,%nameInputNoExt%_command.bat
      runwait, %nameInputNoExt%_command.bat
      FileDelete, %nameInputNoExt%_command.bat
      FileDelete, %pathInput%
      Filecopy, %pathTemp%, %pathInput%
      FileDelete, %pathTemp%  
      }
      else
      {
        Msgbox, Already converted
      }
  }
}
ExitApp



GuiClose:
ExitApp

ButtonCancel:
;Msgbox, Cancel
ExitApp





你可能感兴趣的:(Autohotkey,DIY)