将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. }

附件列表

Ansi2Utf8 小工具的更多相关文章

  1. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  2. 【原】得心应手小工具开发——IE代理快速切换工具

    一.引入 因为公司里上外网要经常换IE代理地址,每次切换地址都要进到Internet Options里去设置一番,经常切换的话很是麻烦,由于用了点时间作个小工具来方便自己. 二.实现思路 其实思路很简 ...

  3. ContentProvider域名替换小工具

    开发项目域名想怎么换就怎么换,就是这么任性! 这是一个很有意思的小工具! 这是一个方便开发人员和测试人员的小工具!! 吐槽: 一直在做Android开发,一直总有一个问题存在:做自己公司的apk开发时 ...

  4. c 小工具的使用

    1. 这是一个gps 数据过滤的小工具,目的是过滤到gps数据中不符合要求的数据,然后转为json 数据 需要两个小工具 bermuda.c   ------>  过滤一定范围的数据 geo2j ...

  5. 偷懒小工具 - SSO单点登录通用类(可跨域)

    写在前面的话 上次发布过一篇同样标题的文章.但是因为跨域方面做得不太理想.我进行了修改,并重新分享给大家. 如果这篇文章对您有所帮助,请您点击一下推荐.以便有动力分享出更多的"偷懒小工具&q ...

  6. [原创] Delphi小工具(Windows资源管理器右键菜单扩展)

    两个小工具 1. 项目临时文件清理 2. Android Ndk 编译 c/c++ jni 源码工具. 下载后,点击Reg.bat就可以完成注册安装.不需要时点击 UnReg.Bat 就可以删除菜单. ...

  7. 介绍两个Ubuntu上的桌面小工具

    经常使用Windows10,Sticky Notes和壁纸自动切换功能挺好用的.我经常会使用Sticky Notes来记录一些信息,内容是实时保存的,而且启动的时候会自动显示在桌面上.其实Ubuntu ...

  8. WPF根据Oracle数据库的表,生成CS文件小工具

    开发小工具的原因: 1.我们公司的开发是客户端用C#,服务端用Java,前后台在通讯交互的时候,会用到Oracle数据库的字段,因为服务器端有公司总经理开发的一个根据Oracle数据库的表生成的cla ...

  9. WPF开发查询加班小工具

    先说一下,我们公司是六点下班,超过7点开始算加班,但是加班的时间是从六点开始计算,以0.5个小时为计数,就是你到了六点半,不算加班半小时,但是加班到七点半,就是加班了一个半小时. 一.打卡记录 首先, ...

随机推荐

  1. 牛客练习赛25 C 再编号

    解题思路 我们先来观察一下题目中给出的公式 $$a'_i=(\sum_{j=1}^na_j)-a_i$$ 通过这个公式推一下经过再编号后的序列的总和,因为我们推出这个和之后可以进行下一次计算. $$\ ...

  2. Sturts2中Action的搜索顺序

    http://localhost:8080/ProjectName/path1/path2/path3/XX.action 首先会判断以/path1/paht2/path3为namespace的pac ...

  3. Django DTL模板语法中的过滤器

    template_filter_demo 过滤器相关: 一.形式:小写{{ name | lower }} 二.串联:先转义文本到HTML,再转换每行到 <p> 标签{{ my_text| ...

  4. 天猫双11红包前端jQuery

    [01]   浏览器支持:IE10+和其他现代浏览器.   效果图:       步骤:   HTML部分:   <div class="opacity" style=&qu ...

  5. vue.js组件之间的通讯-----父亲向儿子传递数据,儿子接收父亲的数据

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 在JQuery中$(document.body)和这个$("body") 这两的区别在哪里?

    两种写法代表的是同一个对象 $("body") 是一个选择器,jQuery 会从 DOM 顶端开始搜索,直到找到标签为 body 的元素. 而 $(document.body) 中 ...

  7. 洛谷—— P1339 [USACO09OCT]热浪Heat Wave

    P1339 [USACO09OCT]热浪Heat Wave 题目描述 The good folks in Texas are having a heatwave this summer. Their ...

  8. 非常适合新手的jq/zepto源码分析01

    (function(global, factory) { // 查看这里是不是定义成模块,如果定义模块就返回 一个模块 if (typeof define === 'function' &&a ...

  9. C++中const引用的是对象的时候只能调用该对象的f()const方法

    const引用的作用: 1. 避免不必要的复制.  2. 限制不能修改对象. const 引用的是对象时只能访问该对象的const 函数  例: class A { public: void cons ...

  10. Username is not in the sudoers file. This incident will be reported

    type sudo adduser <username> sudo where username is the name of the user you want to add in th ...