Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I want to convert byte[] into string, it working correctly with following line.

byte[] byArr= new byte[10];

string ascii =  Encoding.ASCII.GetString(byArr);


But when the char's int value is greater than 127 (i.e extended ASCII char), then i am getting string with wrong character as compare to ASCII characters. I tried with many encoder classes, but each time i am getting different values i.e. which is not same as values in the ASCII table.

Please help me to solve this issue.

Thanks.
Posted
Updated 8-Sep-12 20:54pm
v2

http://stackoverflow.com/questions/666385/how-can-i-convert-extended-ascii-to-a-system-string[^]
Byte 189 represents a "½" in iso-8859-1 (aka "Latin-1"), so the following is maybe what you want:

C#
var e = Encoding.GetEncoding("iso-8859-1");
var s = e.GetString(new byte[] { 189 });

All strings and chars in .NET are UTF-16 encoded, so you need to use an encoder/decoder to convert anything else, sometimes this is defaulted (e.g. UTF-8 for FileStream instances) but good practice is to always specify.

You will need some form of implicit or (better) explicit metadata to supply you with the information about which encoding.
 
Share this answer
 
Comments
LaxmikantYadav 8-Sep-12 8:11am    
hello kenneth, i tried with iso-8859-1, which returns me a different character as compare to ASCII table for the same int number. for e.g 150 int value represent different character int ASCII table as compare to iso-8859-1
Kenneth Haugland 8-Sep-12 8:15am    
Here is a list of all possible encodings supported:
http://msdn.microsoft.com/en-us/library/system.text.encoding.getencodings.aspx

Just a matter of finding the right one :)
It's better to use base64 encoding : Convert.ToBase64String Method (Byte[])[^]
 
Share this answer
 
v2
Comments
Kenneth Haugland 8-Sep-12 8:02am    
Your link links to this question :)
CodeHawkz 8-Sep-12 11:49am    
Fixed it for him :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900