Welcome to MSDN Blogs Sign in | Join | Help

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…

Published Monday, February 20, 2006 6:20 AM by irenak

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

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

Wednesday, February 22, 2006 12:53 AM by Hasani Blackwell
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?

# SYSK 66 Reply to Hasani Blackwell

Thursday, February 23, 2006 8:31 AM by irenak

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.

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

Thursday, March 09, 2006 10:26 AM by Ian Horwill
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.

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

Wednesday, February 14, 2007 5:46 AM by Lasse Laurila

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!

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

Wednesday, April 11, 2007 12:12 PM by Nirav Patel

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!

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

Wednesday, April 11, 2007 12:26 PM by Nirav Patel

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.

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker