C# compare different Encoding pattern between UTF8 and UTF32 based on Md5

 1 using System;
 2 using System.Text;
 3 using System.IO;
 4 using System.Security.Cryptography;
 5 
 6  static void Main(string[] args)
 7         {
 8             CompareFileGetBytes("lyf.txt");
 9             Console.ReadLine();
10         }
11 
12  static void CompareFileGetBytes(string fileFullName)
13         {
14             byte[] fileReadAllBytes = File.ReadAllBytes(fileFullName);
15             string fileReadAllBytesMd5 = GetBytesMd5(fileReadAllBytes);
16 
17             string utf8Md5 = string.Empty;
18             using (StreamReader reader = new StreamReader(fileFullName))
19             {
20                 string textResult = reader.ReadToEnd();
21                 byte[] utf8Bytes = Encoding.UTF8.GetBytes(textResult);
22                 utf8Md5 = GetBytesMd5(utf8Bytes);
23             }
24 
25             string utf32Md5 = string.Empty;
26             using (StreamReader utf32Reader = new StreamReader(fileFullName))
27             {
28                 string textResult = utf32Reader.ReadToEnd();
29                 byte[] utf32Bytes = Encoding.UTF32.GetBytes(textResult);
30                 utf32Md5 = GetBytesMd5(utf32Bytes);
31             }
32 
33 
34             Console.WriteLine($"fileReadAllBytesMd5:{fileReadAllBytesMd5},utf8Md5:{utf8Md5}");
35 
36             if (string.Equals(fileReadAllBytesMd5, utf8Md5))
37             {
38                 Console.WriteLine($"{nameof(fileReadAllBytesMd5)} is equal with {nameof(utf8Md5)}!");
39             }
40             else
41             {
42                 Console.WriteLine($"{nameof(fileReadAllBytesMd5)} is not equal with {nameof(utf8Md5)}!");
43             }
44 
45             Console.WriteLine($"utf8Md5:{utf8Md5},utf32Md5:{utf32Md5}");
46             if (string.Equals(utf8Md5, utf32Md5))
47             {
48                 Console.WriteLine($"{nameof(utf8Md5)} is equals with {nameof(utf32Md5)}");
49             }
50             else
51             {
52                 Console.WriteLine($"{nameof(utf8Md5)} is not  equals with {nameof(utf32Md5)}");
53             }
54         }
55 
56         static string GetBytesMd5(byte[] bytesData)
57         {
58             StringBuilder md5Builder = new StringBuilder();
59             using(MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider())
60             {
61                 byte[] md5Bytes = md5.ComputeHash(bytesData);
62                 for(int i=0;i<md5Bytes.Length;i++)
63                 {
64                     md5Builder.Append(md5Bytes[i].ToString("x2"));
65                 }
66             }
67             return md5Builder.ToString();
68         }

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!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值