C# compare different Encoding pattern between UTF8 and UTF32 based on Md5
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!
C# compare different Encoding pattern between UTF8 and UTF32 based on Md5的更多相关文章
- Unicode Character Set and UTF-8, UTF-16, UTF-32 Encoding
在计算机内存中,统一使用unicode编码,当需要保存到硬盘或者需要传输的时候,就转换为utf-8编码. 用记事本编辑的时候,从文件读取的utf-8字符被转换为unicode字符到内存里,编码完成保存 ...
- 细说:Unicode, UTF-8, UTF-16, UTF-32, UCS-2, UCS-4
1. Unicode与ISO 10646 全世界很多个国家都在为自己的文字编码,并且互不想通,不同的语言字符编码值相同却代表不同的符号(例如:韩文编码EUC-KR中“한국어”的编码值正好是汉字编码GB ...
- UTF-8, UTF-16, UTF-32 & BOM
FAQ - UTF-8, UTF-16, UTF-32 & BOM http://www.unicode.org/faq/utf_bom.html General questions, rel ...
- UTF-8/UTF-16/UTF-32
UTF-8/UTF-16/UTF-32 一.UTF-8/UTF-16/UTF-32三者的区别 二.BOM的检测与删除 1.用VIM去除<feff>,即 U+FEFF.注意:这是一个字符,而 ...
- Unicode 与 utf8 utf16 utf32的关系
Unicode是计算机领域的一项行业标准,它对世界上绝大部分的文字的进行整理和统一编码,Unicode的编码空间可以划分为17个平面(plane),每个平面包含2的16次方(65536)个码位.17个 ...
- 关于编码:Unicode/UTF-8/UTF-16/UTF-32
关于编码,绕不开下面这些概念 ①Unicode/UTF-8/UTF-16/UTF-32 ②大小端字节序(big-endian/little-endian) ③BOM(Byte Order Mark) ...
- Unicode与UTF-8/UTF-16/UTF-32的区别
Unicode的最初目标,是用1个16位的编码来为超过65000字符提供映射.但这还不够,它不能覆盖全部历史上的文字,也不能解决传输的问题 (implantation head-ache's),尤其在 ...
- UTF8,UTF16,UTF32,UTF16-LE,UTF16-BE,GBK 之间的转换
Unicode是Unicode.org制定的编码标准,目前得到了绝大部分操作系统和编程语言的支持.Unicode.org官方对Unicode的定义是:Unicode provides a unique ...
- Unicode 与 Unicode Transformation Format(UTF,UTF-8 / UTF-16 / UTF-32)
ASCII(American Standard Code for Information Interchange):早期它使用7 bits来表示一个字符,总共表示27 = 128个字符:后来扩展到8 ...
随机推荐
- python文件内容处理(一)
综述:一定要理解光标移动的规则 ---------------------------------------------------------------------------第一部分基本操作- ...
- Codeforces_327_C
http://codeforces.com/problemset/problem/327/C 等比求和相加,有mod的出现,所以要算逆元. #include<iostream> #incl ...
- css 浏览兼容问题及解决办法 (1)
主流浏览器css兼容问题的总结 最近又搞了一波网站的兼容,由于要求ie浏览器还是要兼容到ie8,所以调起来还是各种蛋疼. 现在就post一些做兼容的总结,可能不够全面,但是可以告诉大家如何避过一些坑. ...
- mybatis级联查询,多对一查询问题
在使用Mybatis进行多表级联查询时遇到了一个问题:查询结果只有一项,但正确结果是两项.经测试,SQL语句本身没有问题. 在SQL映射文件(XML)中: <!-- 级联查询数据 --> ...
- VFP调用API来控制USB摄像头,实现拍照或录像
*--前提:VFP7.0以上;Windows 2K及以上*--控件:AVICAP32.DLL *--定义:一般放到主程序或表单(集)的Load事件中Public WM_CAP_DRIVER_DISCO ...
- 大数四则运算之减法运算-----c语言版
/* 分三种情况: 1.减数长度大于被减数 交换减数与被减数,输出负号,方便减 2.减数长度等于被减数(分三种情况) a.减数大于被减数,类似1情况1 b.减数等于被减数,两数相等,直接输出0,完成. ...
- centos7使用MySQL的Yum存储库安装mysql5.7.27
下载yum源 官网地址:http://dev.mysql.com/downloads/repo/yum/ centos7系统: http://dev.mysql.com/get/mysql57-com ...
- http各个版本(1/1.1/2)对比
参考的文章: 从理论到实践 全面理解HTTP/2 HTTP协议以及HTTP2.0/1.1/1.0区别 综合阐述http1.0/1.1/2和https 目录: http1.1 长连接 HTTP 1.1支 ...
- 【5min+】后台任务的积木。.NetCore中的IHostedService
系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...
- R语言入门:向量的运算
向量之间的加减乘除运算: > x <- 1 > x [1] 1 2 3 4 5 6 7 8 9 10 > x=x+1 > x [1] 2 3 4 5 6 7 8 9 10 ...