zoukankan      html  css  js  c++  java
  • 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. }

    附件列表

    • 相关阅读:
      setTimeout的时间设为0的问题
      nodejs的简单服务器程序
      使用Promise规定来处理ajax请求的结果
      使用myfocus制作焦点图
      给Array添加删除重复元素函数
      css派生选择器
      Javascript 参数传递
      Node.js 搞Javascript开发的无论如何要尝试一下
      CSS九宫格带边框的多种实现
      80%人会答错的JS基础面试题
    • 原文地址:https://www.cnblogs.com/Hichy/p/7131302.html
    Copyright © 2011-2022 走看看