473,836 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.NET Framework Equivalent to ASCW & ChrW

I would really rather use the "dot-nettified" syntax rather than digging
global functions out of Microsoft.Visua lBasic.

Anyone know the newfangled way to achieve the same results?

--
Peace & happy computing,

Mike Labosh, MCSD
"Musha ring dum a doo dum a da!" -- James Hetfield
Nov 21 '05 #1
17 5820
>I would really rather use the "dot-nettified" syntax rather than digging
global functions out of Microsoft.Visua lBasic.

Anyone know the newfangled way to achieve the same results?


Well if you really want to you can use the System.Convert class.

But I would think twice before using that over ChrW/AscW. They are
compile-time evaluated expressions that result in code that's more
efficient than any function call ever can be.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
I agree. AscW and ChrW are reliable functions that are the most elegant way
to do this. For instance, Convert.ToInt32 will do what AscW does, but you
will have to ensure you're passing exactly one character - AscW takes care of
that for you by only looking at the first character if you pass a string.

You could write your own AscW and ChrW functions using 'pure' .NET code, but
what's the point? (very well tested versions already exist)
--
David Anton
www.tangiblesoftwaresolutions.com
Home of:
Clear VB: Cleans up outdated VB.NET code
Instant C#: Converts from VB.NET to C#
Instant VB: Converts from C# to VB.NET
Instant J#: Converts from VB.NET to J#
"Mattias Sjögren" wrote:
I would really rather use the "dot-nettified" syntax rather than digging
global functions out of Microsoft.Visua lBasic.

Anyone know the newfangled way to achieve the same results?


Well if you really want to you can use the System.Convert class.

But I would think twice before using that over ChrW/AscW. They are
compile-time evaluated expressions that result in code that's more
efficient than any function call ever can be.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #3
On 2005-08-30, David Anton <Da********@dis cussions.micros oft.com> wrote:
I agree. AscW and ChrW are reliable functions that are the most elegant way
to do this. For instance, Convert.ToInt32 will do what AscW does, but you
will have to ensure you're passing exactly one character - AscW takes care of
that for you by only looking at the first character if you pass a string.


That's not exactly what Mattias was pointing out. While there are
functions named AscW and ChrW in the Strings namespace, that function call
is not necessarily what you get when you type AscW("C") into your code.
Rather, they are compile time expressions that result in different
compiled code depending on what parameter types you send to them. (I
don't really know what to call them, they function like keywords, but
they aren't keywords).

The equivalent to the ChrW function call is System.Convert. ToChar().
AscW is more interesting. There really isn't a .Net equivalent since
most .Net languages don't need a function call here, but as you point
out, Convert.ToInt32 will do more or less the same thing with single
characters.
You could write your own AscW and ChrW functions using 'pure' .NET code, but

what's the point? (very well tested versions already exist)


Well, if you switch .Net languages a lot or work in a multi-language shop,
there's an advantage to staying away from the VisualBasic namespace and
the VB conversion keywords. It's a huge namespace with many useful
functions that are similar but not quite identical to the "main"
namespaces of .Net. If you use the same functions across languages,
then you don't have to worry about the subtle differences.

Nov 21 '05 #4
David,
| For instance, Convert.ToInt32 will do what AscW does, but
Consider the case where you pass a literal or constant to AscW or ChrW.

In the case of a literal or constant AscW & ChrW are evaluated at compile
time, there is no function called! Only "pure" inline IL opcodes!

In the case of a char or integer variables AscW is emitted as IL code.

In the case of a Char variable ChrW is emitted as a library call, even then
its a library of .NET functions. My understanding is so as to ensure
negative values are converted into their corresponding "unsigned value".

In the case of String variables are AscW & ChrW is emitted as a library
call, even then its a library of .NET functions. As you stated, this is to
return the first character.
See below for IL code:

//000065:
//000066: Dim ch As Char
//000067: Dim charCode As Integer
//000068: Dim s As String
//000069:
//000070: ch = ControlChars.Lf
IL_0016: ldc.i4.s 10
IL_0018: stloc.0
//000071:
//000072: s = ch
IL_0019: ldloc.0
IL_001a: call string
[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Compiler Services.String Type::FromChar( char)
IL_001f: stloc.2
//000073:
//000074: charCode = AscW(s)
IL_0020: ldloc.2
IL_0021: call int32
[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Strings: :AscW(string)
IL_0026: stloc.1
//000075:
//000076: charCode = AscW(ch)
IL_0027: ldloc.0
IL_0028: stloc.1
//000077:
//000078: ch = ChrW(charCode)
IL_0029: ldloc.1
IL_002a: call char
[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Strings: :ChrW(int32)
IL_002f: stloc.0
//000079:
//000080: s = ChrW(charCode)
IL_0030: ldloc.1
IL_0031: call char
[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Strings: :ChrW(int32)
IL_0036: call string
[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Compiler Services.String Type::FromChar( char)
IL_003b: stloc.2
//000081:
| You could write your own AscW and ChrW functions using 'pure' .NET code,
but
| what's the point? (very well tested versions already exist)
Do people really believe that AscW & ChrW are not "pure" .NET code?

What an odd mis-conception people must have of the framework. :-|

Hope this helps
Jay

"David Anton" <Da********@dis cussions.micros oft.com> wrote in message
news:06******** *************** ***********@mic rosoft.com...
|I agree. AscW and ChrW are reliable functions that are the most elegant
way
| to do this. For instance, Convert.ToInt32 will do what AscW does, but you
| will have to ensure you're passing exactly one character - AscW takes care
of
| that for you by only looking at the first character if you pass a string.
|
| You could write your own AscW and ChrW functions using 'pure' .NET code,
but
| what's the point? (very well tested versions already exist)
| --
| David Anton
| www.tangiblesoftwaresolutions.com
| Home of:
| Clear VB: Cleans up outdated VB.NET code
| Instant C#: Converts from VB.NET to C#
| Instant VB: Converts from C# to VB.NET
| Instant J#: Converts from VB.NET to J#
|
|
| "Mattias Sjögren" wrote:
|
| > >I would really rather use the "dot-nettified" syntax rather than
digging
| > >global functions out of Microsoft.Visua lBasic.
| > >
| > >Anyone know the newfangled way to achieve the same results?
| >
| > Well if you really want to you can use the System.Convert class.
| >
| > But I would think twice before using that over ChrW/AscW. They are
| > compile-time evaluated expressions that result in code that's more
| > efficient than any function call ever can be.
| >
| >
| > Mattias
| >
| > --
| > Mattias Sjögren [MVP] mattias @ mvps.org
| > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
| > Please reply only to the newsgroup.
| >
Nov 21 '05 #5
I wasn't disagreeing with anything Mattias said.
I'm not disputing the compile-time nature of the calls.
I was just stating another reason that someone might want to use AscW and
ChrW rather than trying to find 'pure' .NET equivalents.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
"dfoster" wrote:
On 2005-08-30, David Anton <Da********@dis cussions.micros oft.com> wrote:
I agree. AscW and ChrW are reliable functions that are the most elegant way
to do this. For instance, Convert.ToInt32 will do what AscW does, but you
will have to ensure you're passing exactly one character - AscW takes care of
that for you by only looking at the first character if you pass a string.


That's not exactly what Mattias was pointing out. While there are
functions named AscW and ChrW in the Strings namespace, that function call
is not necessarily what you get when you type AscW("C") into your code.
Rather, they are compile time expressions that result in different
compiled code depending on what parameter types you send to them. (I
don't really know what to call them, they function like keywords, but
they aren't keywords).

The equivalent to the ChrW function call is System.Convert. ToChar().
AscW is more interesting. There really isn't a .Net equivalent since
most .Net languages don't need a function call here, but as you point
out, Convert.ToInt32 will do more or less the same thing with single
characters.
You could write your own AscW and ChrW functions using 'pure' .NET code, but

what's the point? (very well tested versions already exist)


Well, if you switch .Net languages a lot or work in a multi-language shop,
there's an advantage to staying away from the VisualBasic namespace and
the VB conversion keywords. It's a huge namespace with many useful
functions that are similar but not quite identical to the "main"
namespaces of .Net. If you use the same functions across languages,
then you don't have to worry about the subtle differences.

Nov 21 '05 #6
As I mentioned in my other post, I'm not disputing this at all. See my
original post.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
"Jay B. Harlow [MVP - Outlook]" wrote:
David,
| For instance, Convert.ToInt32 will do what AscW does, but
Consider the case where you pass a literal or constant to AscW or ChrW.

In the case of a literal or constant AscW & ChrW are evaluated at compile
time, there is no function called! Only "pure" inline IL opcodes!

In the case of a char or integer variables AscW is emitted as IL code.

In the case of a Char variable ChrW is emitted as a library call, even then
its a library of .NET functions. My understanding is so as to ensure
negative values are converted into their corresponding "unsigned value".

In the case of String variables are AscW & ChrW is emitted as a library
call, even then its a library of .NET functions. As you stated, this is to
return the first character.
See below for IL code:

//000065:
//000066: Dim ch As Char
//000067: Dim charCode As Integer
//000068: Dim s As String
//000069:
//000070: ch = ControlChars.Lf
IL_0016: ldc.i4.s 10
IL_0018: stloc.0
//000071:
//000072: s = ch
IL_0019: ldloc.0
IL_001a: call string
[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Compiler Services.String Type::FromChar( char)
IL_001f: stloc.2
//000073:
//000074: charCode = AscW(s)
IL_0020: ldloc.2
IL_0021: call int32
[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Strings: :AscW(string)
IL_0026: stloc.1
//000075:
//000076: charCode = AscW(ch)
IL_0027: ldloc.0
IL_0028: stloc.1
//000077:
//000078: ch = ChrW(charCode)
IL_0029: ldloc.1
IL_002a: call char
[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Strings: :ChrW(int32)
IL_002f: stloc.0
//000079:
//000080: s = ChrW(charCode)
IL_0030: ldloc.1
IL_0031: call char
[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Strings: :ChrW(int32)
IL_0036: call string
[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Compiler Services.String Type::FromChar( char)
IL_003b: stloc.2
//000081:
| You could write your own AscW and ChrW functions using 'pure' .NET code,
but
| what's the point? (very well tested versions already exist)
Do people really believe that AscW & ChrW are not "pure" .NET code?

What an odd mis-conception people must have of the framework. :-|

Hope this helps
Jay

"David Anton" <Da********@dis cussions.micros oft.com> wrote in message
news:06******** *************** ***********@mic rosoft.com...
|I agree. AscW and ChrW are reliable functions that are the most elegant
way
| to do this. For instance, Convert.ToInt32 will do what AscW does, but you
| will have to ensure you're passing exactly one character - AscW takes care
of
| that for you by only looking at the first character if you pass a string.
|
| You could write your own AscW and ChrW functions using 'pure' .NET code,
but
| what's the point? (very well tested versions already exist)
| --
| David Anton
| www.tangiblesoftwaresolutions.com
| Home of:
| Clear VB: Cleans up outdated VB.NET code
| Instant C#: Converts from VB.NET to C#
| Instant VB: Converts from C# to VB.NET
| Instant J#: Converts from VB.NET to J#
|
|
| "Mattias Sjögren" wrote:
|
| > >I would really rather use the "dot-nettified" syntax rather than
digging
| > >global functions out of Microsoft.Visua lBasic.
| > >
| > >Anyone know the newfangled way to achieve the same results?
| >
| > Well if you really want to you can use the System.Convert class.
| >
| > But I would think twice before using that over ChrW/AscW. They are
| > compile-time evaluated expressions that result in code that's more
| > efficient than any function call ever can be.
| >
| >
| > Mattias
| >
| > --
| > Mattias Sjögren [MVP] mattias @ mvps.org
| > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
| > Please reply only to the newsgroup.
| >

Nov 21 '05 #7
>I wasn't disagreeing with anything Mattias said.
I'm not disputing the compile-time nature of the calls.
I was just stating another reason that someone might want to use AscW and
ChrW rather than trying to find 'pure' .NET equivalents.


But the [OP] would prefer to avoid the Microsoft.Visua lBasic Namespace.
That was the original point.

--
Peace & happy computing,

Mike Labosh, MCSD
"Musha ring dum a doo dum a da!" -- James Hetfield
Nov 21 '05 #8
This is becoming worse than an Abbot & Costello routine... (that dates me)

The original poster wants to avoid the VisualBasic namespace - agreed.
I just gave a reason why you might not feel compelled to do this (as does
Mattias, Jay, etc.).

It is perfectly valid to suggest B when someone is asking for a way to do A
- it's up to the original poster to evaluate the reasoning and choose A or B.

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
"Mike Labosh" wrote:
I wasn't disagreeing with anything Mattias said.
I'm not disputing the compile-time nature of the calls.
I was just stating another reason that someone might want to use AscW and
ChrW rather than trying to find 'pure' .NET equivalents.


But the [OP] would prefer to avoid the Microsoft.Visua lBasic Namespace.
That was the original point.

--
Peace & happy computing,

Mike Labosh, MCSD
"Musha ring dum a doo dum a da!" -- James Hetfield

Nov 21 '05 #9
David,
I was just stating another reason that someone might want to use AscW and
ChrW rather than trying to find 'pure' .NET equivalents.
--


AscW and ChrW are 'pure' .Net, you are mixing up the namespace system.Net
with the .Net framework, somethig the same as some people think that .Net is
only for InterNet.

Cor
Nov 21 '05 #10

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

Similar topics

2
2144
by: Job Lot | last post by:
I have an enumeration as follows Public Enum Delimiters Tab Semicolon Comma Space End Enum How can I return character equivalent of the elements in the enumeration?
6
9460
by: VB Programmer | last post by:
I have a line of code in my project: mobjText.Append(ChrW(Bytes(iIndex))) I took out the reference to Microsoft.VisualBasic in my imports so that I am forced to use all VB.NET stuff. What is the replacement for the ChrW function? Thanks, Robert
7
2592
by: Zytan | last post by:
I am trying to set a const string made up of Chr(), but if the value passed into Chr() is too large, the compiler complains that it is not constant!! Example: Public Const m_Data As String = Chr(&HD) & Chr(&HE) 'ok Public Const m_Data As String = Chr(&HD0) & Chr(&HE0) 'not ok But, Char stores 0..255, so it should be ok in both cases. What's going on?
17
4751
by: AWW | last post by:
Using Textbox.Text = "123" & CStr(13) & "456" I expect 2 lines on the screen but get 1 line with 13 in it. It worked once in a RichTextBox but then stopped. I just want to display aligned tabular data on the screen using CR & Tabs to create data columns. I am an old programmer but .Net novice.<g> Help.
3
10002
Scott Price
by: Scott Price | last post by:
Hello again... This time I'm trying to get M$ Access 2003 VBA to accept the ChrW(215b) (this is the unicode number for the 1/8 character) standard... Each time I try to enter this, it beeps and gives me an error message " Compile Error Expected list separator or )" Other unicode standard characters without letters in them are accepted fine... (for example the unicode number for the 1/3 character is 2153) Does anyone know how to get ChrW...
25
1602
by: cmdolcet69 | last post by:
Public Value As Integer = 4096 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim bytes As Byte() = BitConverter.GetBytes(Value) End Sub This code will create a aan array of 4 bytes {lenght=4} (0) = 0
6
11070
by: =?Utf-8?B?UmljaGFyZA==?= | last post by:
VB has the AscW function. There is no corresponding C# function. How can I do this in C#? I do not want to reference the Microsoft.VisualBasic namespace for just this function.
4
3453
by: David Griffiths | last post by:
Hi all I thought I would convert an old program to use just .net, removing the Microsoft.Visual Basic namespace. I seem to have succeeded except for just one are. What is the .net equivalent to chr(). I am capturing the keyboard input and if it's a carriage return I call the Caluculate Subroutine. Private Sub txtVat_KeyPress(ByVal sender As Object, ByVal e As
2
4109
by: Joe Duchtel | last post by:
Hello - I was wondering why I can do a ... Public Const A As Char = Chr(36) .... but not a ... Public Const B As Char = Chr(165)? It seems like 0-127 are okay with the Chr(). Is this because values > 127 depend on the currently active code page and hence are dynamic?
0
9816
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10840
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9371
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7790
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6978
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5647
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.