Years ago, when I was learning how to use C# to calculate hash values, I learned that to properly output the byte array of a hash (or any other byte array) you had to iterate each byte and call byte.ToString(“X2”) method for the proper formatting, adding each byte string to a StringBuilder.
StringBuilder hash = new StringBuilder(); foreach (byte b in hashBytes) hash.Append(b.ToString("X2"));
Not long ago I was doing something similar and realized that you could use Linq’s Aggregate operator for the same thing in a more concise syntax.
string hash = hashBytes.Aggregate (new StringBuilder(), (sb,b)=>sb.Append(b.ToString("X2") ), sb=>sb.ToString());
But today I was reading the MSDN documentation on BitConverter and saw that there is a method to convert a byte array to a string. I tried it out to see how it compared to the output I was familiar with and saw it was the same except with dashes in between each byte string.
string hash = BitConverter.ToString(hashBytes);
Of course, if you don’t want the dashes, it seems the easiest thing to do would be to replace all dashes with null.
string hash = BitConverter.ToString(hashBytes).Replace("-", null);