HelpNeeder
HelpNeeder

Reputation: 6490

Replace method doesn't replace dashes to empty string

I'm trying to hash a file using SHA1. The result looks like this: B7-DB-B9-93-E7-2F-6F-EB-6D-CD-CC-A8-DE-D2-F1-01-6E-8A-53-BC

How to I replace dashes to empty string or just remove them?

The code trying to replace the dashes, but it seems like it don't change anything and dashes are still in place.

using (HashAlgorithm hashSHA1 = new SHA1Managed())
using (Stream file = new FileStream(ofdBrowse.FileName, FileMode.Open, FileAccess.Read))
{
    byte[] hash = hashSHA1.ComputeHash(file);

    txtSHA1.Text = BitConverter.ToString(hash).Replace("-", "");
}

Upvotes: 0

Views: 1795

Answers (2)

Issa Fram
Issa Fram

Reputation: 2634

Difference between dash and hyphen? http://msdn.microsoft.com/en-us/library/3a733s97.aspx

Not really sure. Just my guess in the dark.

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1502606

The code you've give definitely removes the dashes. Short but complete program to demonstrate that:

using System;
using System.IO;
using System.Security.Cryptography;

class Test
{
    static void Main(string[] args)
    {
        using (HashAlgorithm hashSHA1 = new SHA1Managed())
        {
            // Actual data doesn't matter
            using (Stream data = new MemoryStream())
            {
                byte[] hash = hashSHA1.ComputeHash(data);

                Console.WriteLine(BitConverter.ToString(hash).Replace("-", ""));
            }
        }
    }
}

So, potential cause of your problem:

  • You're not running the build you think you are
  • You've got other code which does the hashing but doesn't have the Replace call
  • You're looking at the wrong bit of the UI :)

It's hard to really guess which of those (or anything else) is the problem, but that code isn't it...

Upvotes: 2

Related Questions