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

J

Jon Skeet [C# MVP]

On Jun 30, 6:32 am, Tom Shelton <[email protected]>
wrote:

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
 
F

Fred

Dans : Tom Shelton écrivait :
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];

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

Jon Skeet [C# MVP]

Tom Shelton said:
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];"
 
C

Ciaran O''Donnell

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

Cor Ligthert[MVP]

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
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
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.
 
C

Cor Ligthert[MVP]

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
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
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.
 
P

Pavel Minaev

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

Pavel Minaev

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

Nathan Sokalski

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
(e-mail address removed)
http://www.nathansokalski.com/

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

Nathan Sokalski

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
(e-mail address removed)
http://www.nathansokalski.com/

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

Pavel Minaev

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

Moonhyuk Choi

When I get final alphabet(e)'s ASC code,
I'm doing like this..

string txt = "Someone";
Int32 charCode = Convert.ToInt32(Convert.ToChar(txt.Substring(txt.Length - 1, 1)));



Nathan Sokalski wrote:

C# equivelant of VB.NET's Asc()
27-Jun-08

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 Sokalsk
(e-mail address removed)
http://www.nathansokalski.com/

Previous Posts In This Thread:

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 Sokalsk
(e-mail address removed)
http://www.nathansokalski.com/

Re: C# equivelant of VB.NET's Asc()
Nathan Sokalski wrote

(int)

Arn

PS: I believe Asc is a VB6'ism.

Re: C# equivelant of VB.NET's Asc()
Asc() can still be used VB.Net. It was in VB3-VB6 and QuickBasic too.

Re: C# equivelant of VB.NET's Asc()
Mr. Arnold ?crivait

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

-
Fre
(e-mail address removed)

Re: C# equivelant of VB.NET's Asc()
Fred ?crivait

PS : Arne's solution is equivalent to Asc

-
Fre
(e-mail address removed)

Re: C# equivelant of VB.NET's Asc()
What does that have to do with anything? it is not a VB6'ism is all that wa
being pointed out here and nothing else.

Re: C# equivelant of VB.NET's Asc()
<snipped

Yeah -- yeah do not be going off the deep-end with this.

Re: C# equivelant of VB.NET's Asc()
Dans : 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

--
Fre
(e-mail address removed)

'"I believe Asc is a VB6'ism"It would have been if you needed to set a
'"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

ht

Michel Posset
 
M

Mike Lovell

When I get final alphabet(e)'s ASC code,
I'm doing like this..

string txt = "Someone";
Int32 charCode = Convert.ToInt32(Convert.ToChar(txt.Substring(txt.Length -
1, 1)));



Nathan Sokalski wrote:

C# equivelant of VB.NET's Asc()
27-Jun-08

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?

Encoding.ASCII.GetBytes("someone")

Assuming the input IS indeed ASCII. This gets you a byte array the same
size as the string, with the character codes in ASCII.
 

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