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!

 

原文链接: https://www.cnblogs.com/Fred1987/p/12129976.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/402065

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:34
下一篇 2023年4月19日 上午9:34

相关推荐