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!

最新文章

  1. 小结RunLoop
  2. while循环语句的使用
  3. vim常用命令汇总
  4. tcp/IP点对点通信程序
  5. 说说GET和POST方法的区别
  6. ZOJ 1092 Arbitrage
  7. DNS子域委派配置案例[转载]
  8. JAVA_Gson_example
  9. weekend110(Hadoop)的 第五天笔记
  10. Qlik报表开发见解
  11. Java实现2048小游戏【代码】
  12. Ext Js v6.2.0.103 Sencha Cmd 命令
  13. 火狐l浏览器所有版本
  14. 通过Python、BeautifulSoup爬取Gitee热门开源项目
  15. python+selenium的web自动化测试之二(Jenkins自动执行)
  16. 使用VS2015编译xlslib库
  17. js求渐升数的第100位
  18. Microsoft Visual Studio2013安装及单元测试
  19. ubuntu18.04智能拼音候选字体调节方法
  20. python string 模块

热门文章

  1. 接口自动化CSV文件生成超长随机字符串--java接口方法
  2. 一接口自动化中生成测试数据需要用到的java类API--import java.util.Properties;
  3. HDU_5094_dfs
  4. Tomcat配置多线程和配置数据库连接池
  5. ansible hosts配置
  6. 网站windows可以访问mac和linux无法访问【MTU MSS问题】
  7. df du 文件空间管理 命令
  8. [Python]获取字典所有值
  9. Java基于过滤器进行重定向不成功问题的兩種解決辦法,以及基於JSF的ajax重定向解決辦法
  10. 【React Native】使用react-native-wechat 进行微信好友、微信朋友圈进行分享