Click here to Skip to main content
Rate this: bad
good
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.
 
Thanks.
Posted 8-Sep-12 2:57am
Edited 8-Sep-12 21:54pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It's better to use base64 encoding : Convert.ToBase64String Method (Byte[])[^]
  Permalink  
v2
Comments
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
good
Please Sign up or sign in to vote.

Solution 2

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:
 
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.
  Permalink  
Comments
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:
http://msdn.microsoft.com/en-us/library/system.text.encoding.getencodings.aspx
 
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 Sergey Alexandrovich Kryukov 370
1 DamithSL 235
2 OriginalGriff 215
3 Zoltán Zörgő 124
4 Dave Kreskowiak 120
0 OriginalGriff 7,610
1 DamithSL 5,559
2 Sergey Alexandrovich Kryukov 5,324
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 9 Sep 2012
Copyright © CodeProject, 1999-2014
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