473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
nj********@hotmail.com
http://www.nathansokalski.com/
Jun 28 '08 #1
47 5417
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.
Jun 28 '08 #2

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@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

Arne

PS: I believe Asc is a VB6'ism.
Asc() can still be used VB.Net. It was in VB3-VB6 and QuickBasic too.

Jun 28 '08 #3

"Fred" <fo*****@free.fr.invalidwrote in message
news:eP**************@TK2MSFTNGP06.phx.gbl...
Dans : news:OS**************@TK2MSFTNGP06.phx.gbl,
Mr. Arnold écrivait :
>"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@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

Arne

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

Jun 28 '08 #4

"Fred" <fo*****@free.fr.invalidwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...

<snipped>

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

Jun 28 '08 #5
'"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.dkschreef in bericht
news:48***********************@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

Arne

PS: I believe Asc is a VB6'ism.

Jun 28 '08 #6
Nathan-

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

-Cor

"Nathan Sokalski" <nj********@hotmail.comschreef in bericht
news:%2****************@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?
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Jun 28 '08 #7
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

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

Jun 28 '08 #8
"Nathan Sokalski" <nj********@hotmail.comschrieb:
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/>

Jun 28 '08 #9
Fred wrote:
Dans : news:eP**************@TK2MSFTNGP06.phx.gbl,
Fred écrivait :
>Dans : news:OS**************@TK2MSFTNGP06.phx.gbl,
Mr. Arnold écrivait :
>>"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@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
Jun 28 '08 #10
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
Jun 28 '08 #11
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/R...utMethod.aspx).
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
Fred wrote:
>Dans : news:eP**************@TK2MSFTNGP06.phx.gbl,
Fred écrivait :
>>Dans : news:OS**************@TK2MSFTNGP06.phx.gbl,
Mr. Arnold écrivait :
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@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

Jun 28 '08 #12
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)
{
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
Jun 28 '08 #13
Yes, it is, I have completely tested the VB.NET version and everything works
perfectly and as I expected.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
"Nathan Sokalski" <nj********@hotmail.comschrieb:
>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/>

Jun 28 '08 #14
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/R...utMethod.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
Jun 28 '08 #15

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2***************@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/R...utMethod.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#.

Jun 28 '08 #16
On Jun 28, 7:16*pm, "Nathan Sokalski" <njsokal...@hotmail.comwrote:
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.
Jun 28 '08 #17

"Arne Vajhøj" <ar**@vajhoej.dkschreef in bericht
news:48***********************@news.sunsite.dk...
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


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
Jun 28 '08 #18
On Sat, 28 Jun 2008 11:09:41 -0400, Arne Vajhøj <ar**@vajhoej.dk>
wrote:
>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
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

Jun 28 '08 #19
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
Jun 28 '08 #20
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
Jun 28 '08 #21

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.dkschreef in bericht
news:48***********************@news.sunsite.dk...
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

Jun 28 '08 #22
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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Pavel Minaev" <in****@gmail.comwrote in message
news:16**********************************@l42g2000 hsc.googlegroups.com...
On Jun 28, 7:16 pm, "Nathan Sokalski" <njsokal...@hotmail.comwrote:
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.
Jun 28 '08 #23
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
nj********@hotmail.com
http://www.nathansokalski.com/

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@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?
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Jun 29 '08 #24
Nathan Sokalski <nj********@hotmail.comwrote:
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
Jun 29 '08 #25
On Sat, 28 Jun 2008 15:20:45 -0400, Arne Vajhøj <ar**@vajhoej.dk>
wrote:
>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
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

Jun 29 '08 #26
On Sun, 29 Jun 2008 08:48:23 +0200, Joergen Bech
<jbech<NOSPAM>@<NOSPAM>post1.tele.dkwrote:

---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/de...t.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

Jun 29 '08 #27
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



Jun 29 '08 #28
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" <ms****@posseth.comschreef in bericht
news:%2****************@TK2MSFTNGP05.phx.gbl...
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


Jun 29 '08 #29
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:
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" <ms****@posseth.comschreef in bericht
news:%2****************@TK2MSFTNGP05.phx.gbl...
>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


Jun 29 '08 #30

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

On Sun, 29 Jun 2008 18:10:20 +0200, "Cor Ligthert[MVP]"
<no************@planet.nlwrote:
>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" <ms****@posseth.comschreef in bericht
news:%2****************@TK2MSFTNGP05.phx.gbl...
>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


Jun 29 '08 #31
"Joergen Bech @ post1.tele.dk>" <jbech<NOSPAMNOSPAMschrieb:
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.

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

Jun 29 '08 #32
On 2008-06-29, Michel Posseth <ms**@posseth.comwrote:
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
Jun 30 '08 #33
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
Jun 30 '08 #34
On 2008-06-30, Jon Skeet [C# MVP] <sk***@pobox.comwrote:
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
Jun 30 '08 #35
Tom Shelton <to*********@comcastXXXXXXX.netwrote:
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];"

--
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
Jun 30 '08 #36
On 2008-06-30, Jon Skeet C# MVP <sk***@pobox.comwrote:
Tom Shelton <to*********@comcastXXXXXXX.netwrote:
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
Jun 30 '08 #37
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?
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Jul 4 '08 #38
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
Jul 5 '08 #39
Cor Ligthert[MVP] <no************@planet.nlwrote:
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.

--
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
Jul 5 '08 #40
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.comschreef in bericht
news:MP*********************@msnews.microsoft.com. ..
Cor Ligthert[MVP] <no************@planet.nlwrote:
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.

--
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
Jul 5 '08 #41
Cor Ligthert[MVP] <no************@planet.nlwrote:
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.

--
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
Jul 5 '08 #42
On Jun 28, 11:19*pm, Arne Vajhøj <a...@vajhoej.dkwrote:
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.
Jul 6 '08 #43
On Jun 29, 12:31*am, "Nathan Sokalski" <njsokal...@hotmail.comwrote:
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).
Jul 6 '08 #44
On Jun 29, 6:05*am, "Nathan Sokalski" <njsokal...@hotmail.comwrote:
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).
Jul 6 '08 #45
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!
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Pavel Minaev" <in****@gmail.comwrote in message
news:63**********************************@k37g2000 hsf.googlegroups.com...
On Jun 29, 12:31 am, "Nathan Sokalski" <njsokal...@hotmail.comwrote:
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).
Jul 6 '08 #46
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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Pavel Minaev" <in****@gmail.comwrote in message
news:63**********************************@k37g2000 hsf.googlegroups.com...
On Jun 29, 12:31 am, "Nathan Sokalski" <njsokal...@hotmail.comwrote:
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).
Jul 7 '08 #47
On Jul 7, 6:16*am, "Nathan Sokalski" <njsokal...@hotmail.comwrote:
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 notcreate
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 aseither
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.
Jul 7 '08 #48

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: Nathan Sokalski | last post by:
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 njsokalski@hotmail.com...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.