Fog Creek Software
Discussion Board

Converting byte[] to string c#

Does anybody know how to covert a byte array to a string in C#?  I want each byte in the array to represent an ASCII character in the resulting string.

I tried using the BitConverter.ToString function but that seemed to convert it to a string of hex values.


Wednesday, July 7, 2004

Perhaps the System.Text.Encoding.ASCII.GetString method, or similar.

Christopher Wells
Wednesday, July 7, 2004

Wednesday, July 7, 2004

Ahh ok.  That'll work.

christopher (
Wednesday, July 7, 2004

yup. everything is in the .net framework somewhere.
actually, beware. ASCII for example is ASCII. that is, nothing above 127 and there is no class to support some other encodings (windows 1252 perhaps, i forget what but know i ran into it sometime)
these are also useful when dealing with streams.

Thursday, July 8, 2004

If you want each byte to be mapped directly to the corresponding Unicode character, use the ISO-8859-1 encoding (aka Latin-1 aka Code Page 28591):


Thursday, July 8, 2004

...but note that this encoding is somewhat different from the popular Windows 1252 ("Western", "Windows ANSI"), just in case that's your string's encoding!

Chris Nahr
Thursday, July 8, 2004

"there is no class to support some other encodings (windows 1252 perhaps..."

Encoding Windows1252Encoding = Encoding.GetEncoding(1252);

Duncan Smart
Thursday, July 8, 2004

thanks for the correction. wonder if i found that when i had the problem, i don't remember the details any more.

Thursday, July 8, 2004

Saturday, July 17, 2004

*  Recent Topics

*  Fog Creek Home