tags:

views:

174

answers:

1

If you are calling ToString on a byte to convert it to a 2 digit hex value does it matter if you use a format provide of CultureInfo.CurrentCulture or CultureInfo.InvariantCulture in any way?

Example:

public string CalculateMD5Hash(string input)
{
    MD5 md5 = System.Security.Cryptography.MD5.Create();
    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
    byte[] hash = md5.ComputeHash(inputBytes);

    StringBuilder sb = new StringBuilder();
    foreach (byte b in hash)
    {
        sb.Append(b.ToString("x2",CultureInfo.InvariantCulture));
    }
    return sb.ToString();
}

Basically can I leave out the IFormatProvider and always get the same result, or if I provide on of the CultureInfo defaults it will make a difference in the output?

+1  A: 

Maybe, in the far far future we will have culturally dependent notations for Hex values. But today, no. You can safely omit the FormatProvider.

Henk Holterman
That is what I thought, but I noticed it in some code I was looking at and it just seemed wrong. Thanks for the confirmation!!
Rodney Foley
There are probably coding standards out there that require to *always* use a FormatProvider in a ToString. Not bad really but you get the occasional overkill.
Henk Holterman