Byte array to string

G

Guest

How to convert this byte array to string
byte[] b=new byte[100];

Is there any function or I need read one by one and build the string

thanks
 
M

Mythran

pkumar said:
How to convert this byte array to string
byte[] b=new byte[100];

Is there any function or I need read one by one and build the string

thanks

byte[] b = new byte[100];
string s = System.Text.ASCIIEncoding.ASCII.GetString(b);


HTH,
Mythran
 
F

Franco, Gustavo

Be carrefull with System.Text.ASCIIEncoding.ASCII.GetString(b);

If your codepage is different than English then the conversion could have
problems (not always).

I had this problem where I had a program running on Korea where the codepage
where different and then the range of Extended ASCII characters supported
are from 0 to 239, instead 0 to 255.

I experimented this problem with some Windows installed on different
languages.

If you want get a string back you can use something like this.

System.Text.Encoding.GetEncoding(1251).GetString(b); (1251 is English)

You can see how the conversion fail if you go to Control Panel, Regional
Settings, Advanced and on "Language for non-Unicode programs" put Japanese.

The strange thing is: clearly it says: "Language for non-Unicode programs",
I know .net is full Unicode.

Now, why using System.Text.ASCIIEncoding.ASCII fail?, I don't know that...

Really doesn't fail, but if you Encode a string with extended characters
into bytes and Decoded again into string, you will get different results. I
guess .net map the extended character to the near one supported for the
codepage.

Gustavo.


Mythran said:
pkumar said:
How to convert this byte array to string
byte[] b=new byte[100];

Is there any function or I need read one by one and build the string

thanks

byte[] b = new byte[100];
string s = System.Text.ASCIIEncoding.ASCII.GetString(b);


HTH,
Mythran
 
J

Jon Skeet [C# MVP]

Franco said:
Be carrefull with System.Text.ASCIIEncoding.ASCII.GetString(b);

If your codepage is different than English then the conversion could have
problems (not always).

I had this problem where I had a program running on Korea where the codepage
where different and then the range of Extended ASCII characters supported
are from 0 to 239, instead 0 to 255.

Careful here - there's no such encoding as "Extended ASCII". There are
various character encodings which *are* extensions to ASCII, but no one
"extended ASCII".
I experimented this problem with some Windows installed on different
languages.

If you want get a string back you can use something like this.

System.Text.Encoding.GetEncoding(1251).GetString(b); (1251 is English)

1251 is just *one* code page...
You can see how the conversion fail if you go to Control Panel, Regional
Settings, Advanced and on "Language for non-Unicode programs" put Japanese.

The strange thing is: clearly it says: "Language for non-Unicode programs",
I know .net is full Unicode.

Yes, but the point is that a byte array isn't an array of characters.
You need to know what encoding the bytes represent characters in, in
order to get from them to Unicode.
Now, why using System.Text.ASCIIEncoding.ASCII fail?, I don't know that...

Because any byte values greater than 127 aren't ASCII.
Really doesn't fail, but if you Encode a string with extended characters
into bytes and Decoded again into string, you will get different results. I
guess .net map the extended character to the near one supported for the
codepage.

You're asking the encoding to deal with bytes which it can't handle -
that's why things go wrong.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top