Click here to Skip to main content
13,290,261 members (52,467 online)
Rate this:
Please Sign up or sign in to vote.
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.

Posted 8-Sep-12 2:57am
Updated 8-Sep-12 21:54pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It's better to use base64 encoding : Convert.ToBase64String Method (Byte[])[^]
Kenneth Haugland 8-Sep-12 8:02am
Your link links to this question :)
CodeHawkz 8-Sep-12 11:49am
Fixed it for him :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2[^]
Byte 189 represents a "½" in iso-8859-1 (aka "Latin-1"), so the following is maybe what you want:

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.
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:

Just a matter of finding the right one :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 9 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100