(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)
--
Fred
fol...@free.fr
PS : Arne's solution is equivalent to AscW
--
Fred
fol...@free.fr
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.
<snipped>
Yeah -- yeah don't be going off the deep-end with this.
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
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...
\\\
int a = Convert.ToInt32('A');
int b = 'A';
///
-Cor
"Nathan Sokalski" <njsok...@hotmail.com> schreef in bericht
news:%23nV5IFM...@TK2MSFTNGP02.phx.gbl...
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;
}
'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/>
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
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
"Arne Vajhøj" <ar...@vajhoej.dk> wrote in message
news:48665320$0$90270$1472...@news.sunsite.dk...
> 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
"Herfried K. Wagner [MVP]" <hirf-spa...@gmx.at> wrote in message
news:%23ekHP9R...@TK2MSFTNGP03.phx.gbl...
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
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#.
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.
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 <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
>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
function(arg) syntax is not OOP.
Arne
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
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
"Arne Vajhøj" <ar...@vajhoej.dk> schreef in bericht
news:48668eb2$0$90275$1472...@news.sunsite.dk...
"Pavel Minaev" <int...@gmail.com> wrote in message
news:16b63401-b971-4bb8...@l42g2000hsc.googlegroups.com...
System.Text.ASCIIEncoding charcode=new System.Text.ASCIIEncoding();
byte asciivalue=charcode.GetBytes(acceptchar)[0];
--
Nathan Sokalski
njsok...@hotmail.com
http://www.nathansokalski.com/
"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?
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
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
---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
---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
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
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...
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:
---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
Whatever distinction there is between a Char and a String in
C# exists in VB.Net as well.
Regards,
Joergen Bech
Note that VB allows an implicit widening conversion between 'Char' and
'String' which C# does not support.
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...
What's wrong with:
return (int)strChar[0];
--
Tom Shelton
<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
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
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
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
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?
Adding a reference, setting the using seems to me much more work then
char A = '65';
So why did you make this reply?
Cor
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.
> 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...
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.
Ada, CLOS and Dylan would strongly disagree.
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).
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).
"Pavel Minaev" <int...@gmail.com> wrote in message
news:63550630-78fb-4086...@k37g2000hsf.googlegroups.com...
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
njsok...@hotmail.com
http://www.nathansokalski.com/
"Pavel Minaev" <int...@gmail.com> wrote in message
news:63550630-78fb-4086...@k37g2000hsf.googlegroups.com...
Well then, judging by these, and my little experiments, a simple
(int)ch will do precisely what is needed.