Ansi2Utf8 小工具

将GB2312编码的文件转成Unity使用的UTF8无bom格式
主要用批处理执行 Ansi2Utf8.exe XXXXX.txt 

源代码
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Ansi2Utf8
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. if (args.Length < 1)
  14. {
  15. Console.WriteLine("None file path !!!");
  16. return;
  17. }
  18. string fileName = args[0];
  19. try
  20. {
  21. FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
  22. if (fs == null)
  23. {
  24. Console.WriteLine(fileName + " is null !!!");
  25. return;
  26. }
  27. byte[] flieByte = new byte[fs.Length];
  28. fs.Read(flieByte, 0, flieByte.Length);
  29. fs.Close();
  30. if (IsUtf8(flieByte))
  31. {
  32. Console.WriteLine(fileName + " is utf8 coding");
  33. return;
  34. }
  35. Encoding ansi = Encoding.GetEncoding("GB2312");
  36. Encoding utf = Encoding.UTF8;
  37. flieByte = Encoding.Convert(ansi, utf, flieByte);
  38. StreamWriter docWriter;
  39. var utf8WithoutBom = new UTF8Encoding(false);
  40. docWriter = new StreamWriter(fileName, false, utf8WithoutBom);
  41. docWriter.Write(utf.GetString(flieByte));
  42. docWriter.Close();
  43. }
  44. catch
  45. {
  46. Console.WriteLine(fileName + " convert error !!!!!!!!!!!!!!");
  47. }
  48. }
  49. static bool IsUtf8(byte[] bs)
  50. {
  51. int len = bs.Length;
  52. if (len >= 3 && bs[0] == 0xEF && bs[1] == 0xBB && bs[2] == 0xBF)
  53. {
  54. return true; //Encoding.UTF8;
  55. }
  56. int[] cs = { 7, 5, 4, 3, 2, 1, 0, 6, 14, 30, 62, 126 };
  57. for (int i = 0; i < len; i++)
  58. {
  59. int bits = -1;
  60. for (int j = 0; j < 6; j++)
  61. {
  62. if (bs[i] >> cs[j] == cs[j + 6])
  63. {
  64. bits = j;
  65. break;
  66. }
  67. }
  68. if (bits == -1)
  69. {
  70. return false; //Encoding.Default;
  71. }
  72. while (bits-- > 0)
  73. {
  74. i++;
  75. if (i == len || bs[i] >> 6 != 2)
  76. {
  77. return false; //Encoding.Default;
  78. }
  79. }
  80. }
  81. return true; //Encoding.UTF8;
  82. }
  83. }
  84. }

附件列表

     

    posted @ 2017-07-07 11:28  Hichy  阅读(603)  评论(0编辑  收藏  举报