How to format md5.ComputeHash for console window without question marks

I have a simple code to create a checksum for files to compare if a file is new or not.

class Program
{
    static void Main(string[] args)
    {
        using (var md5 = MD5.Create())
        {
            using (var stream = File.OpenRead("output_log.txt"))
            {
                var checksum = BitConverter.ToString(md5.ComputeHash(stream))
                                           .Replace("-", "‌​")
                                           .ToLower();
                System.Diagnostics.Debug.WriteLine(checksum);
                Console.Write(checksum);
            }
        }
    }
}

Outputs:

Debug.WriteLine :

A4 88 announcements a7 cc db a7 31 25 a9 e0 44 75 44 86 7s

Console.Write

a4 ?? 88 ???? ad a7 ?? cc ?? db ?? a7 ?? 31 ?? 25 ?? a9 ?? e0 ?? 44 ?? 75 ?? 44 ?? 86 ?? 7s

I think the output of the MD5 digest function is a binary sequence of bytes, not a printed string of characters.

How to print md5 digest as human readable representation as result in Debug.WriteLine?

+4
source share
1 answer

I can’t say exactly why, but it does the replacement of the external line of BitConverter.

var checksum = BitConverter.ToString(md5.ComputeHash(stream))                                               
                           .ToLower();
System.Diagnostics.Debug.WriteLine(checksum);
checksum = checksum.Replace("-", "");
Console.Write(checksum);

. @Hans Passant , . "" Unicode.

:

d41d8cd98f00b204e9800998ecf8427e

: - , , , .

0

Source: https://habr.com/ru/post/1687247/


All Articles