SYSK 66: .NET 1.1 and 2.0 Handles ASCII Encoding Differently?


My tests show that the answer is Yes…  See it for yourself.


Create a windows app in VS 2003, add one text box and one button to a form and put the following code behind the button click:



// A bunch of bytes represented by their code
string[] input = "98 22 248 167 95 213 187 61 95 34 182 249 149 140 222 222 63 192 134 194".Split(' ');


byte[] byteData = new byte[input.Length];
for (int i = 0; i < byteData.Length; i++ )
{
byteData[i] = (byte) int.Parse(input[i]);
}


// this works the same in 1.1 and 2.0
//textBox21Text = System.Text.Encoding.UTF8.GetString(byteData);


// this works the same in 1.1 and 2.0
//textBox1.Text = System.Text.Encoding.Unicode.GetString(byteData);


// this is where the problem surfaces
textBox1.Text = System.Text.Encoding.ASCII.GetString(byteData);


Now, do exactly the same in VS 2005.


Run both applications, click the button and compare the results in the text box.  If you want to go fancy, add a second text box and a second button to VS 2005 project and do the comparison in code as follows:




if (textBox1.Text == textBox2.Text)
 MessageBox.Show("Ok.  Same.");
else
 MessageBox.Show("Not the Same!");


Then, simply copy and paste VS2003’s text box data in text box 2 of VS 2005 project and click the second button to see the results…


Comments (6)

  1. Hasani Blackwell says:

    I don’t have vs2005 or .net 2.0.. is it possible you can post what System.Text.Encoding.ASCII.GetString(byteData);

    returns in 2.0?

  2. irenake says:

    I’m getting an error posting the reply due to non-printable characters.  If interested, send me an e-mail, and I’ll reply directly.

  3. Ian Horwill says:

    An observation: your input string contains invalid values as far as ASCII is concerned, which is a 7-bit code – anything above 127 is invalid.

  4. Lasse Laurila says:

    This is an old post, but I still comment since I just spent two hours debugging my (perfectly working!) code because of this.

    The difference is that .NET 1.1 just spins the 7-bit characters around when high-ascii (or unicode) character is found. 128 becomes 0, 129 becomes 1 and so forth. 2.0 changes >127 character to a question mark (‘?’).

    Gee, thanks Microsoft, way to go!

  5. Nirav Patel says:

    I am facing a odd issue while converting high-ascii charactor ‘206’. I have .Net 1.1 and 2.0 installed in my PC. I compiled a .Net dll to convert ASCII charactors. I am calling this .Neet dll from one .Net application and the other Vb6 application. When I call it from .Net application, it converts no 206 to "N" (starts from 0 for 128 and counts onwards for "N") but when I call the same dll from VB6 code, it converts it to "?". How come same dll produce two different outputs for same no 206?

    Thanks!

  6. Nirav Patel says:

    I forget to add in my previous question that my .Net dll is compiled in v1.1 which is converting ASCII. The .Net program calling this dll is also written in v1.1.

    Note that no 206 is just an example, otherwise it behaves same for all high-ascii characters.

Skip to main content