Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

12 views
Skip to first unread message

Nathan Sokalski

unread,
Jun 27, 2008, 9:50:03 PM6/27/08
to
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?
--
Nathan Sokalski
njsok...@hotmail.com
http://www.nathansokalski.com/


Arne Vajhøj

unread,
Jun 27, 2008, 9:56:55 PM6/27/08
to
Nathan Sokalski wrote:
> 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.

Arnold@arnold.com Mr. Arnold

unread,
Jun 28, 2008, 1:40:52 AM6/28/08
to

"Arne Vajhøj" <ar...@vajhoej.dk> wrote in message
news:48659a67$0$90274$1472...@news.sunsite.dk...

Asc() can still be used VB.Net. It was in VB3-VB6 and QuickBasic too.

Fred

unread,
Jun 28, 2008, 1:54:49 AM6/28/08
to
Dans : news:OSp7aGO2...@TK2MSFTNGP06.phx.gbl,
Mr. Arnold écrivait :

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

--
Fred
fol...@free.fr

Fred

unread,
Jun 28, 2008, 2:00:11 AM6/28/08
to
Dans : news:ePaA8NO2...@TK2MSFTNGP06.phx.gbl,
Fred écrivait :

PS : Arne's solution is equivalent to AscW

--
Fred
fol...@free.fr

Arnold@arnold.com Mr. Arnold

unread,
Jun 28, 2008, 2:03:02 AM6/28/08
to

"Fred" <fol...@free.fr.invalid> wrote in message
news:ePaA8NO2...@TK2MSFTNGP06.phx.gbl...

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.

Arnold@arnold.com Mr. Arnold

unread,
Jun 28, 2008, 2:06:04 AM6/28/08
to

"Fred" <fol...@free.fr.invalid> wrote in message
news:%233qn7QO...@TK2MSFTNGP03.phx.gbl...

<snipped>

Yeah -- yeah don't be going off the deep-end with this.

Fred

unread,
Jun 28, 2008, 2:22:20 AM6/28/08
to
Dans : news:%23Lr1ySO...@TK2MSFTNGP04.phx.gbl,
Mr. Arnold écrivait :

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.

--
Fred
fol...@free.fr

Michel Posseth [MCP]

unread,
Jun 28, 2008, 2:52:08 AM6/28/08
to
'"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
.

"Arne Vajhøj" <ar...@vajhoej.dk> schreef in bericht
news:48659a67$0$90274$1472...@news.sunsite.dk...

Cor Ligthert[MVP]

unread,
Jun 28, 2008, 4:10:28 AM6/28/08
to
Nathan-

\\\
int a = Convert.ToInt32('A');
int b = 'A';
///

-Cor

"Nathan Sokalski" <njsok...@hotmail.com> schreef in bericht
news:%23nV5IFM...@TK2MSFTNGP02.phx.gbl...

Joergen Bech <jbechpost1.tele.dk>

unread,
Jun 28, 2008, 4:18:20 AM6/28/08
to
On Fri, 27 Jun 2008 21:56:55 -0400, Arne Vajhøj <ar...@vajhoej.dk>
wrote:

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;
}

Herfried K. Wagner [MVP]

unread,
Jun 28, 2008, 9:03:04 AM6/28/08
to
"Nathan Sokalski" <njsok...@hotmail.com> schrieb:

> 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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Arne Vajhøj

unread,
Jun 28, 2008, 11:05:05 AM6/28/08
to
Fred wrote:
> Dans : news:ePaA8NO2...@TK2MSFTNGP06.phx.gbl,
> Fred écrivait :
>> Dans : news:OSp7aGO2...@TK2MSFTNGP06.phx.gbl,
>> Mr. Arnold écrivait :
>>> "Arne Vajhøj" <ar...@vajhoej.dk> wrote in message
>>> news:48659a67$0$90274$1472...@news.sunsite.dk...
>>>> Nathan Sokalski wrote:
>>>>> 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
>>>>
>>>> PS: I believe Asc is a VB6'ism.
>>>
>>> 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)
>
> 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

Arne Vajhøj

unread,
Jun 28, 2008, 11:09:41 AM6/28/08
to
Michel Posseth [MCP] wrote:
> '"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

Nathan Sokalski

unread,
Jun 28, 2008, 11:16:55 AM6/28/08
to
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).

"Arne Vajhøj" <ar...@vajhoej.dk> wrote in message
news:48665320$0$90270$1472...@news.sunsite.dk...

Arne Vajhøj

unread,
Jun 28, 2008, 11:20:54 AM6/28/08
to
Joergen Bech <jbech<NOSPAM>@ wrote:
> On Fri, 27 Jun 2008 21:56:55 -0400, Arne Vajhøj <ar...@vajhoej.dk>
> wrote:
>> Nathan Sokalski wrote:
>>> 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

> 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)

Yuck - a piece of code.

Arne

Nathan Sokalski

unread,
Jun 28, 2008, 11:21:54 AM6/28/08
to
Yes, it is, I have completely tested the VB.NET version and everything works
perfectly and as I expected.

"Herfried K. Wagner [MVP]" <hirf-spa...@gmx.at> wrote in message
news:%23ekHP9R...@TK2MSFTNGP03.phx.gbl...

Arne Vajhøj

unread,
Jun 28, 2008, 11:30:09 AM6/28/08
to
Nathan Sokalski wrote:
> 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

Arnold@arnold.com Mr. Arnold

unread,
Jun 28, 2008, 11:37:01 AM6/28/08
to

"Nathan Sokalski" <njsok...@hotmail.com> wrote in message
news:%23ZXOAIT...@TK2MSFTNGP06.phx.gbl...

> 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#.

Pavel Minaev

unread,
Jun 28, 2008, 12:03:13 PM6/28/08
to
On Jun 28, 7:16 pm, "Nathan Sokalski" <njsokal...@hotmail.com> wrote:
> 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.

Michel Posseth [MCP]

unread,
Jun 28, 2008, 12:29:00 PM6/28/08
to

"Arne Vajhøj" <ar...@vajhoej.dk> schreef in bericht
news:48665434$0$90270$1472...@news.sunsite.dk...

No it is VB syntax that is something completely different as compatibility,
it is pointed out several times by the Development team of VB
that all compatibility dll`s are hosted in the
Microsoft.VisualBasic.Compatibility namespace what is the "normal" VB
namespace is VB syntax the essence of the language the thing that makes
VB.Net .

I also do not see your OOP point as the VB namespace is a classic example of
good usage of OOP , and if you do not want to have the helper methods and
contstants that VB provides you have the posibility to remove the VB
namespace completely , however this will make VB a non rad dev environment
just as all other "new" languages out there remember that VB has a proven
language history as a rad and that the complete late binding mechanism of
VB is part of the VB namespace .


regards

Michel
.


Michel


Joergen Bech <jbechpost1.tele.dk>

unread,
Jun 28, 2008, 2:50:08 PM6/28/08
to
On Sat, 28 Jun 2008 11:20:54 -0400, Arne Vajhøj <ar...@vajhoej.dk>
wrote:

>Joergen Bech <jbech<NOSPAM>@ wrote:
>> On Fri, 27 Jun 2008 21:56:55 -0400, Arne Vajhøj <ar...@vajhoej.dk>
>> wrote:
>>> Nathan Sokalski wrote:
>>>> 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
>
>> 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 ...

The poster specifically asked how he could convert the Asc
function to C#. That, to me, suggests that he wants to translate
existing, working code, rather than creating something new from
scratch in order to satisfy someone's requirements for doing things
the "right" way.

If he wants *exactly* the same conversion, he can copy the
whole function (thereby avoiding having to include the namespace)
or - if the characters are known to be <0x80, get equivalent
functionality by grabbing the first part only.

So as I see it, there are two discussions going on in this thread:
How to do a direct translation or how to obtain similar results
some other way. Any of the latter would require testing and
documentation of differences in behavior.

If the requirement is Asc with Asc being what it is, then I fail
to see how int(c) alone would satisfy this requirement?!?

Do you dislike code examples in general or just that piece
of code?

I did not write it. That is what it looks like in the framework
(never mind the variable names).

Regards,

Joergen Bech

Joergen Bech <jbechpost1.tele.dk>

unread,
Jun 28, 2008, 2:58:33 PM6/28/08
to
On Sat, 28 Jun 2008 11:09:41 -0400, Arne Vajhøj <ar...@vajhoej.dk>
wrote:

>Michel Posseth [MCP] wrote:

Asc(char) is a static/shared method of the
Microsoft.VisualBasic.Strings class, so saying that
it is a procedural leftover in an object oriented world
is the same as saying that all public static/shared
methods are procedural leftovers.

I am fairly sure that it would be difficult to write a
pure oo program given such a criteria.

Regards,

Joergen Bech

Arne Vajhøj

unread,
Jun 28, 2008, 3:19:16 PM6/28/08
to
Michel Posseth [MCP] wrote:
> I also do not see your OOP point as the VB namespace is a classic example of
> good usage of OOP ,

function(arg) syntax is not OOP.

Arne

Arne Vajhøj

unread,
Jun 28, 2008, 3:20:45 PM6/28/08
to
Joergen Bech <jbech<NOSPAM>@ wrote:
> On Sat, 28 Jun 2008 11:09:41 -0400, Arne Vajhøj <ar...@vajhoej.dk>
> wrote:
>> But to me it is still a function that only exists for compatibility
>> reasons and is a procedural leftover in an object oriented world.
>
> Asc(char) is a static/shared method of the
> Microsoft.VisualBasic.Strings class, so saying that
> it is a procedural leftover in an object oriented world
> is the same as saying that all public static/shared
> methods are procedural leftovers.
>
> I am fairly sure that it would be difficult to write a
> pure oo program given such a criteria.

When I call a shared method in VB.NET i use the syntax:

classname.methodname(argument)

are you saying that I in general can just use methodname(argument) ??

Arne

Arne Vajhøj

unread,
Jun 28, 2008, 3:23:36 PM6/28/08
to

In general I think it is bad to port a pre-.NET design to
.NET 1:1.

> If the requirement is Asc with Asc being what it is, then I fail
> to see how int(c) alone would satisfy this requirement?!?

I don't think anyone has said anything differently.

That piece of code.

Arne

Michel Posseth [MCP]

unread,
Jun 28, 2008, 4:16:05 PM6/28/08
to

Well there is lack of an agreed-upon and rigorous definition of OOP
and method(arg) syntax seems to be one of them

"Arne Vajhøj" <ar...@vajhoej.dk> schreef in bericht

news:48668eb2$0$90275$1472...@news.sunsite.dk...

Nathan Sokalski

unread,
Jun 28, 2008, 4:31:21 PM6/28/08
to
You say that the value is "compared to a char value". Would you mind telling
me where I do this? If you are thinking of typedchar, that is not a char
(actually, it doesn't even exist in the final result). In the last few lines
of my code, you will notice that I replace the text "typedchar" with either
event.keyCode or event.which, depending on the browser. If you look at what
values the event.keyCode and event.which have, you will see why I do not
want the Unicode value.

"Pavel Minaev" <int...@gmail.com> wrote in message
news:16b63401-b971-4bb8...@l42g2000hsc.googlegroups.com...

Nathan Sokalski

unread,
Jun 28, 2008, 10:05:40 PM6/28/08
to
I haven't tested it yet, but does anybody think the following would work:

System.Text.ASCIIEncoding charcode=new System.Text.ASCIIEncoding();
byte asciivalue=charcode.GetBytes(acceptchar)[0];

"Nathan Sokalski" <njsok...@hotmail.com> wrote in message
news:%23nV5IFM...@TK2MSFTNGP02.phx.gbl...


> 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?

Jon Skeet [C# MVP]

unread,
Jun 29, 2008, 2:39:30 AM6/29/08
to
Nathan Sokalski <njsok...@hotmail.com> wrote:
> I haven't tested it yet, but does anybody think the following would work:
>
> System.Text.ASCIIEncoding charcode=new System.Text.ASCIIEncoding();
> byte asciivalue=charcode.GetBytes(acceptchar)[0];

For genuinely ASCII characters, that will give the same result as just
casting the character. For non-ASCII characters, I believe it will
return 3F ('?').

(If I *were* to use ASCIIEncoding, I'd suggest the Encoding.ASCII
property instead of creating a new one, btw.)

--
Jon Skeet - <sk...@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com

Joergen Bech <jbechpost1.tele.dk>

unread,
Jun 29, 2008, 2:48:23 AM6/29/08
to
On Sat, 28 Jun 2008 15:20:45 -0400, Arne Vajhøj <ar...@vajhoej.dk>
wrote:

An example from the VB world:

---snip---
Imports System.Drawing.Graphics
...
Private Sub Test()
Dim bm As New Bitmap("c:\test.bmp")
Dim g As Graphics = FromImage(bm) '<<<<<<<<<<<<
End Sub
---snip---

instead of

---snip---
Private Sub Test()
Dim bm As New Bitmap("c:\test.bmp")
Dim g As Graphics = Graphics.FromImage(bm) '<<<<<<<<<
End Sub
---snip---

When you create a WinForms VB project, System.Drawing is already
included as a reference, but System.Drawing.Graphics is not.

In the last example above, you would have to prefix the Graphics
class to access the shared method.

One thing I will admit, though: Asc can be accessed using
Microsoft.VisualBasic.Asc and
Microsoft.VisualBasic.Strings.Asc.

Strings is a module, so in this case I suppose you are right,
though in practical terms I do not see much of a difference
between accessing Asc from a module and - as in the example
above - FromImage directly from a class by importing the
Graphics namespace.

Regards,

Joergen Bech

Joergen Bech <jbechpost1.tele.dk>

unread,
Jun 29, 2008, 3:02:22 AM6/29/08
to
On Sat, 28 Jun 2008 15:23:36 -0400, Arne Vajhøj <ar...@vajhoej.dk>
wrote:

---snip---

>In general I think it is bad to port a pre-.NET design to
>.NET 1:1.

Agreed. And on the same note, Microsoft should never have
provided a VB6->VB.Net upgrade wizard, but I suppose they
had to in order to give (some) VB6 developers the illusion that
VB6 to VB.Net was a natural migration path and keep them
from jumping to something else entirely.

>> If the requirement is Asc with Asc being what it is, then I fail
>> to see how int(c) alone would satisfy this requirement?!?
>
>I don't think anyone has said anything differently.

Nope.

---snip---

>>> Yuck - a piece of code.
>>
>> Do you dislike code examples in general or just that piece
>> of code?
>
>That piece of code.

OK. Plenty where that came from.

/Joergen Bech

Joergen Bech <jbechpost1.tele.dk>

unread,
Jun 29, 2008, 3:41:04 AM6/29/08
to
On Sun, 29 Jun 2008 08:48:23 +0200, Joergen Bech
<jbech<NOSPAM>@<NOSPAM>post1.tele.dk> wrote:

---snip---

>One thing I will admit, though: Asc can be accessed using
>Microsoft.VisualBasic.Asc and
>Microsoft.VisualBasic.Strings.Asc.
>
>Strings is a module, so in this case I suppose you are right,
>though in practical terms I do not see much of a difference
>between accessing Asc from a module and - as in the example
>above - FromImage directly from a class by importing the
>Graphics namespace.

Doh! Remind me not to post on a Sunday. My head is
not working today. Let me correct myself before someone
else does:

Strings is a class, of course - just stuffed in a module.

From http://discuss.joelonsoftware.com/default.asp?dotnet.12.353213.14

"A module IS just a class where Shared is implicitly understood for
each member. And the module name doesn't need to be supplied when the
members are used. That's it."

So Asc is just as OO (or not) as the rest of the framework.
Technically the same. Just a difference of style.

Regards,

Joergen Bech

Michel Posseth

unread,
Jun 29, 2008, 11:57:56 AM6/29/08
to
Nathan Sokalski schreef:

> 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?

Nathan

Currently i am sitting behind my dev laptopn wich has VS.Net 2008 and
Sharp develop installed

one of the nice features of sharp develop is that it can translate code
back and forward from VB to C# or BOO and vice versa ( maybe you should
have a look at it )


when i say in VB.Net

Private Sub test
Dim i As Integer = Asc("A")
End Sub

this is translated to C# and BOO as
C#
private void test()
{
int i = Strings.Asc("A");
}
BOO
private def test():
i as int = Strings.Asc('A')


In VS.Net 2008

this code ( note that i needed the value thrown in asc to be a non
constant value otherwise the IL that is generated is for this one
constant and you do not see the actuall call)

Private Sub test()
Dim i As Integer = Asc(My.Settings.testval)
MsgBox(i.ToString)
End Sub

becomes after compiling and decompiling with Reflector

Private Sub test()

Interaction.MsgBox(Strings.Asc(MySettingsProperty.Settings.testval).ToString,
MsgBoxStyle.OkOnly, Nothing)
End Sub

and in C#

private void test()
{

Interaction.MsgBox(Strings.Asc(MySettingsProperty.Settings.testval).ToString(),
MsgBoxStyle.OkOnly, null);
}

Conclusion ?

Asc(My.Settings.testval) = strings.asc(My.Settings.testval)

HTH

Michel Posseth

Cor Ligthert[MVP]

unread,
Jun 29, 2008, 12:10:20 PM6/29/08
to
Michiel,

I don't know if you are aware that there is in C# a strict distinct between
a char and a string.

Even a sql nchar(1) will in a datacontext be used as a Char

There is in VB as well a Char, however beside the IndexOff in an array I
don't remember that I have ever used that.

"X"c

Cor

"Michel Posseth" <msn...@posseth.com> schreef in bericht
news:%23VbUjDg...@TK2MSFTNGP05.phx.gbl...

Michel Posseth

unread,
Jun 29, 2008, 12:41:40 PM6/29/08
to
Cor ,

Yes i am aware of this fact however the strings.Asc method is overloaded
just as VB`s ASC function so the IDE doesn`t care although i do code
strict

:-)

But for the unknowing it might be good info to know
regards
michel

Cor Ligthert[MVP] schreef:

Joergen Bech <jbechpost1.tele.dk>

unread,
Jun 29, 2008, 2:03:40 PM6/29/08
to
On Sun, 29 Jun 2008 17:57:56 +0200, Michel Posseth
<msn...@posseth.com> wrote:

---snip---

>Conclusion ?
>
>Asc(My.Settings.testval) = strings.asc(My.Settings.testval)
>
>HTH
>
>Michel Posseth

Yes, of course it is.

Asc is a public shared method of the

Microsoft.VisualBasic.String

class.

But to use it in C# you would still have create a reference to the
Microsoft.VisualBasic namespace in order to make use of Asc
(or Strings.Asc, if you like).

Regards,

Joergen Bech

Joergen Bech <jbechpost1.tele.dk>

unread,
Jun 29, 2008, 2:10:50 PM6/29/08
to

A Char is a Char no matter if the language is
C# or VB.Net.

Whatever distinction there is between a Char and a String in
C# exists in VB.Net as well.

Regards,

Joergen Bech

Herfried K. Wagner [MVP]

unread,
Jun 29, 2008, 4:57:10 PM6/29/08
to
"Joergen Bech @ post1.tele.dk>" <jbech<NOSPAMNOSPAM> schrieb:

> A Char is a Char no matter if the language is
> C# or VB.Net.
>
> Whatever distinction there is between a Char and a String in
> C# exists in VB.Net as well.

Note that VB allows an implicit widening conversion between 'Char' and
'String' which C# does not support.

Michel Posseth

unread,
Jun 29, 2008, 5:47:44 PM6/29/08
to
yes it is that or

char[] chrBuffer = { Convert.ToChar(strChar) };
byte[] bytBuffer = Encoding.Default.GetBytes(chrBuffer);
return (int)bytBuffer[0];

michel

"Joergen Be...@post1.tele.dk>" <jbech<NOSPAMNOSPAM> schreef in bericht
news:5fjf64dmk2540g7fa...@4ax.com...

Tom Shelton

unread,
Jun 30, 2008, 1:32:53 AM6/30/08
to
On 2008-06-29, Michel Posseth <ms...@posseth.com> wrote:
> yes it is that or
>
> char[] chrBuffer = { Convert.ToChar(strChar) };
> byte[] bytBuffer = Encoding.Default.GetBytes(chrBuffer);
> return (int)bytBuffer[0];
>

What's wrong with:

return (int)strChar[0];

--
Tom Shelton

Jon Skeet [C# MVP]

unread,
Jun 30, 2008, 2:02:37 AM6/30/08
to
On Jun 30, 6:32 am, Tom Shelton <tom_shel...@comcastXXXXXXX.net>
wrote:

<snip>

> What's wrong with:
>
> return (int)strChar[0];

That will always return the Unicode encoding of the character, which
isn't always the same as the value in the system defaut encoding.

Personally I try to steer clear of the system default encoding as much
as possible, but sometimes it's necessary.

Jon

Fred

unread,
Jun 30, 2008, 2:06:42 AM6/30/08
to
Dans : news:Ee2dnZhugMqY7fXV...@comcast.com,
Tom Shelton écrivait :

It will return the Unicode value of the first char.
Not the system default encoding value as Asc does.
These lines are a part of the complete program given by Joergen in his
first post (reflector).
IMO, if Nathan wants the behavior of Asc, so he should reference the VB
dll in his program and use Asc, or copy the complete code which deals
also with multi-bytes charsets (but not sure that Asc still meets
Nathan's requirements in this case :-) )

--
Fred
fol...@free.fr

Tom Shelton

unread,
Jun 30, 2008, 10:40:38 AM6/30/08
to
On 2008-06-30, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> On Jun 30, 6:32 am, Tom Shelton <tom_shel...@comcastXXXXXXX.net>
> wrote:
>
><snip>
>
>> What's wrong with:
>>
>> return (int)strChar[0];
>
> That will always return the Unicode encoding of the character, which
> isn't always the same as the value in the system defaut encoding.
>

It's pretty much the implementation of the VB.NET function AscW :) Asc does a
little more checking depending on the value of the conversion and the current
encoding.

--
Tom Shelton

Jon Skeet [C# MVP]

unread,
Jun 30, 2008, 2:37:38 PM6/30/08
to
Tom Shelton <tom_s...@comcastXXXXXXX.net> wrote:
> > That will always return the Unicode encoding of the character, which
> > isn't always the same as the value in the system defaut encoding.
>
> It's pretty much the implementation of the VB.NET function AscW :)

Yes.

> Asc does a little more checking depending on the value of the
> conversion and the current encoding.

Exactly - and seeing as the OP wanted an implementation of Asc instead
of Ascw, that's what's wrong with "return (int)strChar[0];"

Tom Shelton

unread,
Jun 30, 2008, 2:53:31 PM6/30/08
to
On 2008-06-30, Jon Skeet C# MVP <sk...@pobox.com> wrote:
> Tom Shelton <tom_s...@comcastXXXXXXX.net> wrote:
>> > That will always return the Unicode encoding of the character, which
>> > isn't always the same as the value in the system defaut encoding.
>>
>> It's pretty much the implementation of the VB.NET function AscW :)
>
> Yes.
>
>> Asc does a little more checking depending on the value of the
>> conversion and the current encoding.
>
> Exactly - and seeing as the OP wanted an implementation of Asc instead
> of Ascw, that's what's wrong with "return (int)strChar[0];"
>

I guess I didn't pay attention to what I was writing :)

--
Tom Shelton

Ciaran O''Donnell

unread,
Jul 4, 2008, 5:56:00 PM7/4/08
to
The have been a lot of answers to this but the shortest is:
1) Add a reference to Microsoft.VisualBasic and the function is
Microsoft.VisualBasic.Strings.Asc(char)

2) Using reflector to get the source to the function in C#, its:


public static int Asc(char theChar)
{
int num;
int num2 = Convert.ToInt32(theChar);


if (num2 < 0x80)
{
return num2;
}
try
{
byte[] buffer;

Encoding fileIOEncoding = Encoding.Default;
char[] chars = new char[] { theChar };


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;
}

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


"Nathan Sokalski" wrote:

> 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?

Cor Ligthert[MVP]

unread,
Jul 5, 2008, 4:37:27 AM7/5/08
to
> The have been a lot of answers to this but the shortest is:
> 1) Add a reference to Microsoft.VisualBasic and the function is
> Microsoft.VisualBasic.Strings.Asc(char)
>
I beg your pardon,

Adding a reference, setting the using seems to me much more work then

char A = '65';

So why did you make this reply?

Cor

Jon Skeet [C# MVP]

unread,
Jul 5, 2008, 5:14:51 AM7/5/08
to
Cor Ligthert[MVP] <notmyfi...@planet.nl> wrote:
> > The have been a lot of answers to this but the shortest is:
> > 1) Add a reference to Microsoft.VisualBasic and the function is
> > Microsoft.VisualBasic.Strings.Asc(char)
> >
> I beg your pardon,
>
> Adding a reference, setting the using seems to me much more work then
>
> char A = '65';

I think you mean

char A = 65;

> So why did you make this reply?

Because (as has been pointed out several times in this thread) just
using the Unicode value is the equivalent of AscW, not Asc.

Cor Ligthert[MVP]

unread,
Jul 5, 2008, 1:18:49 PM7/5/08
to
Jon,

> Because (as has been pointed out several times in this thread) just
> using the Unicode value is the equivalent of AscW, not Asc.

Yes and England was not at Euro 2008

Move your head a little bit up, and read the subject: C# equivelant of
VB.NET's Asc()

Cor

"Jon Skeet [C# MVP]" <sk...@pobox.com> schreef in bericht
news:MPG.22d94be...@msnews.microsoft.com...

Jon Skeet [C# MVP]

unread,
Jul 5, 2008, 1:35:50 PM7/5/08
to
Cor Ligthert[MVP] <notmyfi...@planet.nl> wrote:
> > Because (as has been pointed out several times in this thread) just
> > using the Unicode value is the equivalent of AscW, not Asc.
>
> Yes and England was not at Euro 2008

What on earth has that got to do with anything?

> Move your head a little bit up, and read the subject: C# equivelant of
> VB.NET's Asc()

Indeed. The OP wants the easiest way of achieving in C# what he'd
achieve in VB.NET using Asc. The simplest and most reliable way is to
call the same Asc method that VB.NET uses.

Let's go back to the very first post, quoted here in its entirety (of
the body, anyway):

<quote>


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?

</quote>

The simplest "equivalent function" is to call the original function, is
it not? In other words, Ciaran's first suggestion was entirely
appropriate.

Your suggestion not only wouldn't compile, but certainly wasn't an
"equivalent function", given that it wasn't a function at all.

Pavel Minaev

unread,
Jul 6, 2008, 7:29:51 AM7/6/08
to
On Jun 28, 11:19 pm, Arne Vajhøj <a...@vajhoej.dk> wrote:
> Michel Posseth [MCP] wrote:
> > I also do not see your OOP point as the VB namespace is a classic example of
> > good usage of OOP ,
>
> function(arg) syntax is not OOP.

Ada, CLOS and Dylan would strongly disagree.

Pavel Minaev

unread,
Jul 6, 2008, 7:44:55 AM7/6/08
to
On Jun 29, 12:31 am, "Nathan Sokalski" <njsokal...@hotmail.com> wrote:
> You say that the value is "compared to a char value". Would you mind telling
> me where I do this? If you are thinking of typedchar, that is not a char
> (actually, it doesn't even exist in the final result). In the last few lines
> of my code,  you will notice that I replace the text "typedchar" with either
> event.keyCode or event.which, depending on the browser. If you look at what
> values the event.keyCode and event.which have, you will see why I do not
> want the Unicode value.

Can you please clarify what kind of key codes do event.keyCode /
event.which return? When they correspond to characters, what encoding
are they in? You say that they are not Unicode - but then, what are
they? Certainly not just ASCII, otherwise they wouldn't properly
report non-Latin characters.

If they are in the default system encoding, then obviously it is the
default system encoding of the client (since JavaScript runs on the
client). In which case Asc() is actually incorrect, since it would use
the default system encoding of the server, and they need not match.

Anyway, I've wrote a simple test to see what is there:

<html>
<head>
<script type="text/javascript">
function keyPress() {
var div = document.getElementById("keycode");
div.innerText += event.keyCode;
div.innerText += '\n';
div.innerText += event.which;
div.innerText += '\n\n'
}
</script>
</head>
<body>
<textarea onkeypress="keyPress()"></textarea>
<div id="keycode"></div>
</body>
</html>

And on my system, both IE7 and Opera 9.5 display Unicode codepoints
for Russian characters I input, and not codepoints from my default
ANSI encoding (which would be win1251).

Pavel Minaev

unread,
Jul 6, 2008, 7:49:04 AM7/6/08
to
On Jun 29, 6:05 am, "Nathan Sokalski" <njsokal...@hotmail.com> wrote:
> I haven't tested it yet, but does anybody think the following would work:
>
> System.Text.ASCIIEncoding charcode=new System.Text.ASCIIEncoding();
> byte asciivalue=charcode.GetBytes(acceptchar)[0];

It would, but if you really want to restrict yourself to ASCII, then
you can use a slightly modified original proposal:

char c;
int asciiValue = (sbyte)c;

This is because Unicode codepoints for all symbols in ASCII are
equivalent to their ASCII codes. If you want to guard against non-
ASCII symbols, you could use "checked":

int c;
try {
int asciiValue = checked((sbyte)c);
} catch (OverflowException) {
// Wasn't an ASCII character; handle error
...
}

Or just check for (c <= sbyte.MaxValue).

Nathan Sokalski

unread,
Jul 6, 2008, 2:46:53 PM7/6/08
to
I never had the opportunity to test my stuff on machines other than my own
(although I have tested it being run using my machine as the server and
using my webhost as the server), so you may be right about some of this
stuff. I probably should do a little research on exactly what is returned by
event.keyCode and event.which so that I can hopefully avoid any bugs due to
server/client encoding mismatches. I have never really dealt with character
encoding, so I guess I have my next challenge, and thanks for pointing this
out to me, I appreciate it!

"Pavel Minaev" <int...@gmail.com> wrote in message
news:63550630-78fb-4086...@k37g2000hsf.googlegroups.com...

Nathan Sokalski

unread,
Jul 6, 2008, 10:16:45 PM7/6/08
to
I guess I didn't really answer your question as far as what kind of key
codes event.keyCode & event.which return. After a little research, here is
the best description I can give (I may be slightly off, and with all the
different browsers and versions, it can be hard to cover it all):

event.keyCode:
In Internet Explorer, event.keyCode is the Unicode value of the
character typed (whether it be a, A, tab, enter, etc). Some keys do not even
trigger this event, such as shift & ctrl, which only trigger the keyup &
keydown events.
In Firefox, event.keyCode returns a key code for keys that do not create
a visible character (this includes tab & enter, but not space). For a good
reference, see http://www.quirksmode.org/js/keys.html

event.which:
Internet Explorer does not use event.which
In Firefox, event.which is, based on my observations, the same as either
event.keyCode or event.charCode. Since only one of these two properties is
assigned a value (depending on what key is pressed), that same value is
assigned to event.which. I am guessing that the designers of Firefox did not
intend for event.which to be used, and to have you use only event.keyCode &
event.charCode.

"Pavel Minaev" <int...@gmail.com> wrote in message
news:63550630-78fb-4086...@k37g2000hsf.googlegroups.com...

Pavel Minaev

unread,
Jul 7, 2008, 2:30:29 AM7/7/08
to
On Jul 7, 6:16 am, "Nathan Sokalski" <njsokal...@hotmail.com> wrote:
> I guess I didn't really answer your question as far as what kind of key
> codes event.keyCode & event.which return. After a little research, here is
> the best description I can give (I may be slightly off, and with all the
> different browsers and versions, it can be hard to cover it all):
>
> event.keyCode:
>     In Internet Explorer, event.keyCode is the Unicode value of the
> character typed (whether it be a, A, tab, enter, etc). Some keys do not even
> trigger this event, such as shift & ctrl, which only trigger the keyup &
> keydown events.
>     In Firefox, event.keyCode returns a key code for keys that do not create
> a visible character (this includes tab & enter, but not space). For a good
> reference, seehttp://www.quirksmode.org/js/keys.html

>
> event.which:
>     Internet Explorer does not use event.which
>     In Firefox, event.which is, based on my observations, the same as either
> event.keyCode or event.charCode. Since only one of these two properties is
> assigned a value (depending on what key is pressed), that same value is
> assigned to event.which. I am guessing that the designers of Firefox did not
> intend for event.which to be used, and to have you use only event.keyCode &
> event.charCode.

Well then, judging by these, and my little experiments, a simple
(int)ch will do precisely what is needed.

0 new messages