using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
public class EncodingFormatCheck
{
static void SetEncoding(string filePath)
{
// 读取文件内容
string fileContent = string.Empty;
using (StreamReader sr = new StreamReader(filePath, Encoding.GetEncoding("gbk")))
{
fileContent = sr.ReadToEnd();
}
// 将文件内容转换为utf-8编码格式
byte[] utf8Bytes = Encoding.UTF8.GetBytes(fileContent);
// 将转换后的内容写入文件
using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8))
{
sw.Write(Encoding.UTF8.GetString(utf8Bytes));
}
}
static bool IsUtf8(string filePath)
{
if (!File.Exists(filePath))
{
return true;
}
// 读取文件内容
byte[] bytes = File.ReadAllBytes(filePath);
int i = 0;
while (i < bytes.Length)
{
if ((bytes[i] & 0x80) == 0x00)
{
// 单字节编码,ASCII字符
i += 1;
}
else if ((bytes[i] & 0xE0) == 0xC0)
{
// 双字节编码
if (i + 1 >= bytes.Length || (bytes[i + 1] & 0xC0) != 0x80)
{
return false;
}
i += 2;
}
else if ((bytes[i] & 0xF0) == 0xE0)
{
// 三字节编码
if (i + 2 >= bytes.Length || (bytes[i + 1] & 0xC0) != 0x80 || (bytes[i + 2] & 0xC0) != 0x80)
{
return false;
}
i += 3;
}
else if ((bytes[i] & 0xF8) == 0xF0)
{
// 四字节编码
if (i + 3 >= bytes.Length || (bytes[i + 1] & 0xC0) != 0x80 || (bytes[i + 2] & 0xC0) != 0x80 || (bytes[i + 3] & 0xC0) != 0x80)
{
return false;
}
i += 4;
}
else
{
// 不是UTF-8编码
return false;
}
}
return true;
}
}
改变脚本文件编码格式,包括脚本中的中文注释
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 原创文章,欢迎转载。转载请注明:关东升的博客 前面说到Swift注释的语法有两种:单行注释(//)和多行注释(/....
- Python用‘‘‘ ’’’三引号,不给变量赋值就是多行注释了 Python默认存储格式是UTF-8,如何修改呢?...