Re: Byte array to string
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Tue, 2 Aug 2005 06:54:55 +0100
Franco, Gustavo <gustavo_franco[REMOVEIT]@hotmail.com> wrote:
> 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.
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.
- References:
- Byte array to string
- From: pkumar
- Re: Byte array to string
- From: Mythran
- Re: Byte array to string
- From: Franco, Gustavo
- Byte array to string
- Prev by Date: Re: String Comparison
- Next by Date: Re: Paranthesis in C#
- Previous by thread: Re: Byte array to string
- Next by thread: After using HOOK to catch the KeyPress message,is it possible to modify the content of the message?
- Index(es):
Relevant Pages
|