C# compare different Encoding pattern between UTF8 and UTF32 based on Md5
2024-10-01 04:54:48
using System;
using System.Text;
using System.IO;
using System.Security.Cryptography; static void Main(string[] args)
{
CompareFileGetBytes("lyf.txt");
Console.ReadLine();
} static void CompareFileGetBytes(string fileFullName)
{
byte[] fileReadAllBytes = File.ReadAllBytes(fileFullName);
string fileReadAllBytesMd5 = GetBytesMd5(fileReadAllBytes); string utf8Md5 = string.Empty;
using (StreamReader reader = new StreamReader(fileFullName))
{
string textResult = reader.ReadToEnd();
byte[] utf8Bytes = Encoding.UTF8.GetBytes(textResult);
utf8Md5 = GetBytesMd5(utf8Bytes);
} string utf32Md5 = string.Empty;
using (StreamReader utf32Reader = new StreamReader(fileFullName))
{
string textResult = utf32Reader.ReadToEnd();
byte[] utf32Bytes = Encoding.UTF32.GetBytes(textResult);
utf32Md5 = GetBytesMd5(utf32Bytes);
} Console.WriteLine($"fileReadAllBytesMd5:{fileReadAllBytesMd5},utf8Md5:{utf8Md5}"); if (string.Equals(fileReadAllBytesMd5, utf8Md5))
{
Console.WriteLine($"{nameof(fileReadAllBytesMd5)} is equal with {nameof(utf8Md5)}!");
}
else
{
Console.WriteLine($"{nameof(fileReadAllBytesMd5)} is not equal with {nameof(utf8Md5)}!");
} Console.WriteLine($"utf8Md5:{utf8Md5},utf32Md5:{utf32Md5}");
if (string.Equals(utf8Md5, utf32Md5))
{
Console.WriteLine($"{nameof(utf8Md5)} is equals with {nameof(utf32Md5)}");
}
else
{
Console.WriteLine($"{nameof(utf8Md5)} is not equals with {nameof(utf32Md5)}");
}
} static string GetBytesMd5(byte[] bytesData)
{
StringBuilder md5Builder = new StringBuilder();
using(MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider())
{
byte[] md5Bytes = md5.ComputeHash(bytesData);
for(int i=;i<md5Bytes.Length;i++)
{
md5Builder.Append(md5Bytes[i].ToString("x2"));
}
}
return md5Builder.ToString();
}
I had validated that different encoding mode can generate different result,they are not identical.
Besides,the File.ReadAllBytes may based on UTF8 because they render the identical result!
最新文章
- 小结RunLoop
- while循环语句的使用
- vim常用命令汇总
- tcp/IP点对点通信程序
- 说说GET和POST方法的区别
- ZOJ 1092 Arbitrage
- DNS子域委派配置案例[转载]
- JAVA_Gson_example
- weekend110(Hadoop)的 第五天笔记
- Qlik报表开发见解
- Java实现2048小游戏【代码】
- Ext Js v6.2.0.103 Sencha Cmd 命令
- 火狐l浏览器所有版本
- 通过Python、BeautifulSoup爬取Gitee热门开源项目
- python+selenium的web自动化测试之二(Jenkins自动执行)
- 使用VS2015编译xlslib库
- js求渐升数的第100位
- Microsoft Visual Studio2013安装及单元测试
- ubuntu18.04智能拼音候选字体调节方法
- python string 模块
热门文章
- 接口自动化CSV文件生成超长随机字符串--java接口方法
- 一接口自动化中生成测试数据需要用到的java类API--import java.util.Properties;
- HDU_5094_dfs
- Tomcat配置多线程和配置数据库连接池
- ansible hosts配置
- 网站windows可以访问mac和linux无法访问【MTU MSS问题】
- df du 文件空间管理 命令
- [Python]获取字典所有值
- Java基于过滤器进行重定向不成功问题的兩種解決辦法,以及基於JSF的ajax重定向解決辦法
- 【React Native】使用react-native-wechat 进行微信好友、微信朋友圈进行分享