C# equivelant of VB.NET's Asc()

N

Nathan Sokalski

VB.NET has a function, Asc(), that gets the Ascii value of a character. I am
attempting to find an equivelant function for C#. Can somebody help me here?
 
A

Arne Vajhøj

Nathan said:
VB.NET has a function, Asc(), that gets the Ascii value of a character. I am
attempting to find an equivelant function for C#. Can somebody help me here?

(int)c

Arne

PS: I believe Asc is a VB6'ism.
 
F

Fred

Dans : Mr. Arnold écrivait :
Asc() can still be used VB.Net. It was in VB3-VB6 and QuickBasic too.

But it's not equivalent to Arne's solution as it returns windows default
encoding character code (not Unicode value)
 
F

Fred

Dans : Fred écrivait :
Dans : Mr. Arnold écrivait :

But it's not equivalent to Arne's solution as it returns windows
default encoding character code (not Unicode value)

PS : Arne's solution is equivalent to AscW
 
M

Mr. Arnold

Fred said:
Dans : Mr. Arnold écrivait :

But it's not equivalent to Arne's solution as it returns windows default
encoding character code (not Unicode value)

What does that have to do with anything? It's not a VB6'ism is all that was
being pointed out here and nothing else.
 
F

Fred

Dans : Mr. Arnold écrivait :
What does that have to do with anything? It's not a VB6'ism is all
that was being pointed out here and nothing else.

I probably misunderstood as english is not my native language. I just
wanted Nathan to know he can get some different results with (int)c that
he used to get with Asc.
I am sorry to disturb.
 
M

Michel Posseth [MCP]

'"I believe Asc is a VB6'ism"

It would have been if you needed to set a reference to the
Microsoft.VisualBasic.Compatibility namespace
(Microsoft.VisualBasic.Compatiblity.dll)
please note that if you do not need to set this reference you are working
with the Microsoft.VisualBasic namespace wich is for current Visual Basic
..NET programs , it is a mamanged library that is part of the framework just
as system.data for instance, It is even possible to set a reference to the
VB namespace in C# and thus use all the VB shortcut methods in C#

hth

Michel Posseth
..
 
J

Joergen Bech

(int)c

Arne

PS: I believe Asc is a VB6'ism.

If you *know* that your character is in the 0x00-0x7f range,
(int)c will do just fine.

But as you can see from using .Net Reflector (below), the full
Asc(char) function is a little more elaborate than that.

Regards,

Joergen Bech

---snip---

public static int Asc(char String)
{
int num;
int num2 = Convert.ToInt32(String);
if (num2 < 0x80)
{
return num2;
}
try
{
byte[] buffer;
Encoding fileIOEncoding = Utils.GetFileIOEncoding();
char[] chars = new char[] { String };
if (fileIOEncoding.IsSingleByte)
{
buffer = new byte[1];
int num3 = fileIOEncoding.GetBytes(chars, 0, 1, buffer,
0);
return buffer[0];
}
buffer = new byte[2];
if (fileIOEncoding.GetBytes(chars, 0, 1, buffer, 0) == 1)
{
return buffer[0];
}
if (BitConverter.IsLittleEndian)
{
byte num4 = buffer[0];
buffer[0] = buffer[1];
buffer[1] = num4;
}
num = BitConverter.ToInt16(buffer, 0);
}
catch (Exception exception)
{
throw exception;
}
return num;
}
 
H

Herfried K. Wagner [MVP]

Nathan Sokalski said:
VB.NET has a function, Asc(), that gets the Ascii value of a character. I
am attempting to find an equivelant function for C#. Can somebody help me
here?

'Asc' returns the Windows ANSI character code depending on the system's
Windows ANSI codepage. Is this really what you want to do?
 
A

Arne Vajhøj

Fred said:
Dans : Fred écrivait :

PS : Arne's solution is equivalent to AscW

Good point. There is a difference. I think chances are reasonable
good that the original poster want the Unicode value. But it is
obviously something he needs to be aware of.

Arne
 
A

Arne Vajhøj

Michel said:
'"I believe Asc is a VB6'ism"

It would have been if you needed to set a reference to the
Microsoft.VisualBasic.Compatibility namespace
(Microsoft.VisualBasic.Compatiblity.dll)
please note that if you do not need to set this reference you are working
with the Microsoft.VisualBasic namespace wich is for current Visual Basic
.NET programs , it is a mamanged library that is part of the framework just
as system.data for instance, It is even possible to set a reference to the
VB namespace in C# and thus use all the VB shortcut methods in C#

OK.

But to me it is still a function that only exists for compatibility
reasons and is a procedural leftover in an object oriented world.

But that is of course not a technical view.

Arne
 
N

Nathan Sokalski

No, I do not want the Unicode value. I do know the difference between Asc()
and AscW() (Although I do appreciate that you took into account that it is
an easy mistake to make), and I am looking for the equivelant of Asc(). My
planned use is for generating JavaScript (You can see the VB.NET version of
the code I am trying to convert on my site at
http://www.nathansokalski.com/code/RestrictInputMethod.aspx).
 
A

Arne Vajhøj

Joergen Bech said:
If you *know* that your character is in the 0x00-0x7f range,
(int)c will do just fine.

Or if he wants unicode, which you normally would in .NET ...
But as you can see from using .Net Reflector (below), the full
Asc(char) function is a little more elaborate than that.
public static int Asc(char String)
{
int num;
int num2 = Convert.ToInt32(String);
if (num2 < 0x80)
{
return num2;
}
try
{
byte[] buffer;
Encoding fileIOEncoding = Utils.GetFileIOEncoding();
char[] chars = new char[] { String };
if (fileIOEncoding.IsSingleByte)
{
buffer = new byte[1];
int num3 = fileIOEncoding.GetBytes(chars, 0, 1, buffer,
0);
return buffer[0];
}
buffer = new byte[2];
if (fileIOEncoding.GetBytes(chars, 0, 1, buffer, 0) == 1)
{
return buffer[0];
}
if (BitConverter.IsLittleEndian)
{
byte num4 = buffer[0];
buffer[0] = buffer[1];
buffer[1] = num4;
}
num = BitConverter.ToInt16(buffer, 0);
}
catch (Exception exception)
{
throw exception;
}
return num;
}

Yuck - a piece of code.

Arne
 
N

Nathan Sokalski

Yes, it is, I have completely tested the VB.NET version and everything works
perfectly and as I expected.
 
A

Arne Vajhøj

Nathan said:
No, I do not want the Unicode value. I do know the difference between Asc()
and AscW() (Although I do appreciate that you took into account that it is
an easy mistake to make), and I am looking for the equivelant of Asc(). My
planned use is for generating JavaScript (You can see the VB.NET version of
the code I am trying to convert on my site at
http://www.nathansokalski.com/code/RestrictInputMethod.aspx).

Unless you have to support some legacy ASP stuff, then I would
recommend going Unicode internally and UTF-8 externally
for an ASP.NET web app.

Arne
 
M

Mr. Arnold

Nathan Sokalski said:
No, I do not want the Unicode value. I do know the difference between
Asc() and AscW() (Although I do appreciate that you took into account that
it is an easy mistake to make), and I am looking for the equivelant of
Asc(). My planned use is for generating JavaScript (You can see the VB.NET
version of the code I am trying to convert on my site at
http://www.nathansokalski.com/code/RestrictInputMethod.aspx).

You know you could make an Asc() method by using VB.Net, make a method in VB
like MakeAsc() using Asc() within the method, compile it as a Library
solution, set reference to it in your C# project and use your made-up VB
MakeAsc() method from C#.
 
P

Pavel Minaev

No, I do not want the Unicode value. I do know the difference between Asc()
and AscW() (Although I do appreciate that you took into account that it is
an easy mistake to make), and I am looking for the equivelant of Asc(). My
planned use is for generating JavaScript (You can see the VB.NET version of
the code I am trying to convert on my site athttp://www.nathansokalski.com/code/RestrictInputMethod.aspx).

You use Asc() to generate an integer literal that is inserted into
JavaScript, where it is compared to a char value. Given that
JavaScript uses Unicode strings the same way .NET does, it would seem
that you do indeed need the Unicode version, unless I'm missing
something else.

Anyway, assuming you really do need Asc() and nothing else, the C#
equivalent would be Encoding.Default.GetBytes(ch)[0] - but that is
ignoring the fact that "ch" might be a multibyte character.
 

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