How do i convert byte[] to a string

I am trying to Hash a file via the MD5 crypt.
now i get the result in a byte[] how doi display this as a string in a textbox?

thanks and regards.

i got this thus far:
private void Form1_Load(object sender, System.EventArgs e)
                  System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                  FileStream fs = new FileStream("book_01.jpg", FileMode.Open, FileAccess.Read, FileShare.Read);
                  byte[] result = md5.ComputeHash(fs);
                  foreach(byte oneByte in result)
                        this.txtMD5.Text += oneByte.ToString();
                  // txtMD5.Text = Convert.ToBase64String(result);


running with md5sum, clearly i get this:
$ md5sum book_01.jpg
0fd5c55ffdf784e2596fbc0b892e3f96  book_01.jpg

the for each loop does this:

the convert to base64 does this:

Who is Participating?
wellileinConnect With a Mentor Commented:
I think you want the for-loop to produce the same result as md5sum
md5sum outputs the hash in hexadecimal, your code does in decimal.
Have a look at it:
0F D5 C5 ...
15 213 197 ...
So what you need is a converter from decimal to hex. Try this:
public string toHex(byte b)
  char[] hex = "0123456789abcdef".ToCharArray();
  return hex[b/16].ToString() + hex[b%16].ToString();
and change your loop to
foreach(byte oneByte in result)
  this.txtMD5.Text += toHex(oneByte);
I think this is what you are looking for:

PS:you can change the encoding
Fahad MukhtarDistinguished EngineerCommented:
byte[] ByteArray = new byte[100];
string MyString= System.Text.Encoding.ASCII.GetString(ByteArray ,0,ByteArray.Length);//this will actually convert the byte to string
Actually, you look like you're done already.  The Base64 answer that you already posted is your best solution to store your MD5.  I guess it really depends on what you are wanting to do with the result.

> txtMD5.Text = Convert.ToBase64String(result);
> D9XFX/33hOJZb7wLiS4/lg==

Looks like you already have a handle on the answer.
weinrjAuthor Commented:
Brilliant, thanks so much!
All Courses

From novice to tech pro — start learning today.