Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C#
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
Edited 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 at 8-Sep-12 8:02am
Your link links to this question :)
CodeHawkz at 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 at 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 at 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
0 OriginalGriff 605
1 Maciej Los 225
2 Abhinav S 171
3 Richard Deeming 160
4 Suvendu Shekhar Giri 159
0 Sergey Alexandrovich Kryukov 9,623
1 OriginalGriff 9,095
2 Peter Leow 5,044
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 2,581

Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 9 Sep 2012
Copyright © CodeProject, 1999-2015
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