Click here to Skip to main content
15,887,888 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
VB
VB
Dim s(256) As String
Dim i As Integer
For i = 0 To 255
    s(i) = i & "=" & Chr(i)
Next
System.IO.File.WriteAllLines("AsciiVB.txt", s)


C#
C#
string[] s = new string[256];
for (int i = 0; i <= 255; i++)
    s[i] = i.ToString() + "=" + ((char)i).ToString();
System.IO.File.WriteAllLines("AsciiCS.txt", s);


both programs Gives Different Outoputs from 128
ex.
VB
128=€

CS
128
Posted

VB.NET Chr(i) and C# (char)i are not equivalent.

To obtain the same behavior, through VB.NET you have to call ChrW function instead of Chr function.

The first one (ChrW) accepts UNICODE values (between -32768 and 65535), whereas the second (Chr) accepts ASCII values (between 0 and 255).

Internally, ChrW always calls Convert.ToChar, the same as the explicit cast in C#.
Chr calls Convert.ToChar only when the value is between 0 and 127. For greater values it uses the Encoding object for ANSI code pages.

It is explained at Chr, ChrW Functions[^].

If you instead want to obtain in C# the same behavior from VB.NET, then you can:
1) In your C# project, add an assembly reference to Microsoft.VisualBasic, then replace
C#
(char)i
with
C#
Microsoft.VisualBasic.Strings.Chr(i)
.
2) You can emulate Microsoft.VisualBasic.Strings.Chr by replacing the cast with a call to Encoding.GetChars, as follows:
C#
var culture = System.Threading.Thread.CurrentThread.CurrentCulture;
var encoding = Encoding.GetEncoding(culture.TextInfo.ANSICodePage);
char[] buffer = new char[1];
for (int i = 0; i <= 255; i++)
{
    encoding.GetChars(new byte[] { (byte)i }, 0, 1, buffer, 0);
    s[i] = i.ToString() + "=" + buffer[0].ToString();
}

Note that Encoding is defined inside System.Text namespace.

Regards,
Daniele.
 
Share this answer
 
v3
Comments
Sid_Joshi 21-Nov-14 5:37am    
What changes should I do in C# to obtain Same behavior like Char() from Vb
Daniele Rota Nodari 21-Nov-14 6:56am    
I just improved the solution to show you how to obtain that behavior in C#.
Anyhow, I encourage you to adopt in VB.NET the same approach C# is using by default.
More, I suggest you to study all the provided links and the Encoding class.
SoftNax 23-Nov-14 6:52am    
Yes, Daniele is 100% correct,
VB.NET Chr() & C# Char() are not the same.
Sid_Joshi 26-Nov-14 5:08am    
Thanks For Your Solution....
this will Creates Reverse Effect of Your code
byte i1 = Encoding.Default.GetBytes("Œ")[0];
textBox3.Text = i1.ToString();
They are initialized with different locales. ASCII cover only the characters 0-127, all the others are extensions and depend on codepages.

More on http://en.wikipedia.org/wiki/Code_page[^].
 
Share this answer
 
 
Share this answer
 
v2
Comments
Sid_Joshi 26-Nov-14 5:36am    
Thanks for your answer...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900