473,383 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Byte to String

Hello,

I am using the following to hash a string:

public static string Hash(string text) {
MD5CryptoServiceProvider crypto = new
MD5CryptoServiceProvider();
byte[] hash = null;
UTF8Encoding encoder = new UTF8Encoding();
hash = crypto.ComputeHash(encoder.GetBytes(text));
return hash.ToString();
} // Hash

I debugged it but I still get a byte and I can't see the hashed
string ...

What am I missing?

Thanks,
Miguel
Nov 7 '08 #1
3 5050
Hi Miguel,

byte[] is an array and ToString() will just return the type name. Here are a
few examples:

1) return BitConverter.ToString(hash); // e.g. A1-F3-BB...

2) return BitConverter.ToString(hash).Replace("-", ""); // e.g. A1F3BB...

3) string hashString = string.Empty;
Array.ForEach(hash, delegate(byte b) { hashString +=
b.ToString("X2"); });
return hashString; // same as 2)
--
Stanimir Stoyanov
http://stoyanoff.info

"shapper" <md*****@gmail.comwrote in message
news:03**********************************@e1g2000p ra.googlegroups.com...
Hello,

I am using the following to hash a string:

public static string Hash(string text) {
MD5CryptoServiceProvider crypto = new
MD5CryptoServiceProvider();
byte[] hash = null;
UTF8Encoding encoder = new UTF8Encoding();
hash = crypto.ComputeHash(encoder.GetBytes(text));
return hash.ToString();
} // Hash

I debugged it but I still get a byte and I can't see the hashed
string ...

What am I missing?

Thanks,
Miguel
Nov 7 '08 #2
On Nov 7, 12:37*am, "Stanimir Stoyanov" <stoya...@REMOVETHIS.live.com>
wrote:
Hi Miguel,

byte[] is an array and ToString() will just return the type name. Here are a
few examples:

1) return BitConverter.ToString(hash); // e.g. A1-F3-BB...

2) return BitConverter.ToString(hash).Replace("-", ""); // e.g. A1F3BB...

3) string hashString = string.Empty;
* * Array.ForEach(hash, delegate(byte b) { hashString +=
b.ToString("X2"); });
* * return hashString; // same as 2)
--
Stanimir Stoyanovhttp://stoyanoff.info

"shapper" <mdmo...@gmail.comwrote in message

news:03**********************************@e1g2000p ra.googlegroups.com...
Hello,
I am using the following to hash a string:
* *public static string Hash(string text) {
* * *MD5CryptoServiceProvider crypto = new
MD5CryptoServiceProvider();
* * *byte[] hash = null;
* * *UTF8Encoding encoder = new UTF8Encoding();
* * *hash = crypto.ComputeHash(encoder.GetBytes(text));
* * *return hash.ToString();
* *} // Hash
I debugged it but I still get a byte and I can't see the hashed
string ...
What am I missing?
Thanks,
Miguel

Thank You! It worked fine.
Nov 7 '08 #3
Stanimir Stoyanov wrote:
Hi Miguel,

byte[] is an array and ToString() will just return the type name. Here
are a few examples:

1) return BitConverter.ToString(hash); // e.g. A1-F3-BB...

2) return BitConverter.ToString(hash).Replace("-", ""); // e.g. A1F3BB...

3) string hashString = string.Empty;
Array.ForEach(hash, delegate(byte b) { hashString +=
b.ToString("X2"); });
return hashString; // same as 2)
Don't use += to concatenate strings in a loop. Use a StringBuilder instead:

StringBuilder builder = new StringBuilder(hash.Length * 2);
foreach (byte b in hash) builder.Append(b.ToString("x2"));
return builder.ToString();

As the size of the final string is specified in the constructor, it will
allocate a string buffer of the correct size, which then is returned as
a string by the ToString method. (To make it ever more efficient, you
could format the hex string yourself and append one character at a time.
That way it wouldn't create any temporary strings at all.)

4) return Convert.ToBase64String(hash); // e.g. dh1e5gxs4hh3j...
--
Stanimir Stoyanov
http://stoyanoff.info

--
Göran Andersson
_____
http://www.guffa.com
Nov 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
2
by: Jose Perez | last post by:
Dear All, I have the following problem. I am passing an encrypted value on the querystring from one aspx to another. Originally I was outputing characters such as "+, /,..." (which caused...
2
by: Bryan | last post by:
Apologies if this is a noob question, but I've been struggling with this for quite a while... I'm trying to convert a byte array (encrypted authorization code) into a *screen-printable* string...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
4
by: ThunderMusic | last post by:
Hi, I have to go from Byte() to String, do some processing then reconvert the String to byte() but using ascii format, not unicode. I currently use a stream to write the char()...
10
by: Danny | last post by:
I am working on a project where I will receive xml documents from clients machines as a byte array. They will use the web browser navigate method to post the data to my ASP.NET page. I then pick up...
5
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For...
6
by: =?Utf-8?B?TWljaGFlbA==?= | last post by:
Hi, I need to create a formatted byte array for SMPP, e.g.: 00 00 00 21 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 is the length of the entire...
10
by: ShadowLocke | last post by:
I'm looking for a better understanding of whats going on with the code i write. I have come across something that I thought I understood but its not working as expected. Its hard to explain so let me...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.