Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do i convert byte[] to a string

Posted on 2003-10-22
5
Medium Priority
?
1,035 Views
Last Modified: 2010-04-16
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:
152131979525324713222689111188111374663150

the convert to base64 does this:
D9XFX/33hOJZb7wLiS4/lg==

0
Comment
Question by:weinrj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Expert Comment

by:GtG
ID: 9603004
I think this is what you are looking for:
Encoding.ASCII.GetString(byte[])

PS:you can change the encoding
0
 
LVL 19

Expert Comment

by:Fahad Mukhtar
ID: 9604447
byte[] ByteArray = new byte[100];
..............
..............
string MyString= System.Text.Encoding.ASCII.GetString(ByteArray ,0,ByteArray.Length);//this will actually convert the byte to string
0
 
LVL 7

Expert Comment

by:psdavis
ID: 9606051
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.
0
 
LVL 2

Accepted Solution

by:
wellilein earned 500 total points
ID: 9606320
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);
0
 

Author Comment

by:weinrj
ID: 9607792
Brilliant, thanks so much!
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question