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.

Christopher

christopher baus.net
Wednesday, July 07, 2004

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

Christopher Wells
Wednesday, July 07, 2004

system.text.encoding.foo.getstring/getbytes

mb
Wednesday, July 07, 2004

Ahh ok.  That'll work.

christopher (baus.net)
Wednesday, July 07, 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.

mb
Thursday, July 08, 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):

System.Text.Encoding.GetEncoding("iso-8859-1")

Joe
Thursday, July 08, 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 08, 2004

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

Encoding Windows1252Encoding = Encoding.GetEncoding(1252);

Duncan Smart
Thursday, July 08, 2004

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

mb
Thursday, July 08, 2004

http://www.tutorialmaniacs.com/confirm/118/587108819

asdasd
Saturday, July 17, 2004

*  Recent Topics

*  Fog Creek Home