473,529 Members | 2,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# equivilent for Asc and Chr

'Asc' returns an Integer value representing the character code
corresponding to a character

'Chr' is the inverse and returns the character associated with the
specified character code

What are the equivilents in C#?

Jan 4 '06 #1
9 145551
In C#, the `char' data type is an integer type. You can cast them
between each other using normal C# casts. Here's an example:

char b = (char) 64;
int n = b;

Console.WriteLine("Character {0} is {1}", b, n);

Program output:

Character @ is 64

This depends upon you using ASCII. .NET is meant to work with Unicode
natively, so I suspect some complicated Unicode/ASCII conversion is
happening when you say (char) 64. If you wanted to write functions
that look the same as their VB counterparts, you could do that easily:

char Chr(int n) {
return (char) n;
}

There's not really much point to that, though, and you might end up on
thedailywtf.com if you do :)

Jan 4 '06 #2
'Asc' returns an Integer value representing the character code
corresponding to a character

'Chr' is the inverse and returns the character associated with the
specified character code


A simple cast will do in most cases, like (int)'A' or (char)70.

But that will get you the behaviour of VB's ChrW and AscW respectively
(the Unicode versions of the functions). Asc and Chr do ANSI<->Unicode
conversion, so if that's what you really need then you have to use
System.Text.Encoding.Default.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 4 '06 #3
check out the Convert class and BitConverter class.

kind regards,
Bruno.
"INeedADip" <in*******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
'Asc' returns an Integer value representing the character code
corresponding to a character

'Chr' is the inverse and returns the character associated with the
specified character code

What are the equivilents in C#?

Jan 4 '06 #4
Here's my very old quick 'n dirty implementation for these two functions:

public static byte Asc(char src) {
return(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(src +
"")[0]);
}

public static char Chr(byte src) {
return(System.Text.Encoding.GetEncoding("iso-8859-1").GetChars(new byte[]
{src})[0]);
}

C#/.NET does not have a direct equivalent because you can have many
different encondings (the old Asc() and Chr() implied ISO8859P1, but doing
things this way is "old" thinking...
Jan 4 '06 #5
Gabriel Magaña <no*****@no-spam.com> wrote:
Here's my very old quick 'n dirty implementation for these two functions:

public static byte Asc(char src) {
return(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(src +
"")[0]);
}

public static char Chr(byte src) {
return(System.Text.Encoding.GetEncoding("iso-8859-1").GetChars(new byte[]
{src})[0]);
}

C#/.NET does not have a direct equivalent because you can have many
different encondings (the old Asc() and Chr() implied ISO8859P1, but doing
things this way is "old" thinking...


No, they didn't imply ISO-8859-1 - they implied whatever the current
codepage for the thread was, I believe. That's certainly what the
VB.NET documentation states.

(From what I remember, that's not strictly accurate either - I believe
some caching goes on, and the behaviour is very hard to predict. I had
a look at it a while ago, and it's horrible.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 4 '06 #6
>No, they didn't imply ISO-8859-1 - they implied whatever the current
codepage for the thread was, I believe. That's certainly what the
VB.NET documentation states.


Hmm I was talking about VB6 and prior to that... (I guess I qualify as "old
fart" in the computer world, having been programming since the DOS days, but
I'm only 32!)

Anyway, this ambiguity is exactly why, in retrospect, I'm glad Asc() and
Chr() don't exist as built-in functions in C#... I've started to write a
lot of multi-lingual user interfaces and I can really see where code page
assumptions would have screwed me up!
Jan 4 '06 #7
Gabriel Magaña <no*****@no-spam.com> wrote:
No, they didn't imply ISO-8859-1 - they implied whatever the current
codepage for the thread was, I believe. That's certainly what the
VB.NET documentation states.
Hmm I was talking about VB6 and prior to that... (I guess I qualify as "old
fart" in the computer world, having been programming since the DOS days, but
I'm only 32!)


Well, seeing as the VB.NET function is meant to mirror VB6 as far as
possible, I doubt they'd put a code-page dependency in there at this
point - especially when it would actually be *easier* to always use
8859-1.

The documentation I've found on MSDN for Asc isn't specific at all,
unfortunately - do you have any which clear it up for definite either
way? I don't really want to install VB6 to check :)
Anyway, this ambiguity is exactly why, in retrospect, I'm glad Asc() and
Chr() don't exist as built-in functions in C#... I've started to write a
lot of multi-lingual user interfaces and I can really see where code page
assumptions would have screwed me up!


Localisation is a horrible business, made worse by code pages. Heck, it
would be bad enough without things like Unicode surrogates, which just
add to the problems too :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 4 '06 #8
>The documentation I've found on MSDN for Asc isn't specific at all,
unfortunately - do you have any which clear it up for definite either
way? I don't really want to install VB6 to check :)


Hmm information is sketchy... I was never a VB programmer, Asc() and Chr()
seem to be one of theose few functions that are called the same in a bunch
of languages! I did find this:
http://msdn.microsoft.com/library/de...l/vafctchr.asp

Which implies that the value returned is based on the current system
codepage, since it states that "Numbers from 0 31 are the same as standard,
nonprintable ASCII codes. For example, Chr(10) returns a linefeed character.
The normal range for charcode is 0 255. However, on DBCS systems, the actual
range for charcode is -32768 to 65535."
Jan 4 '06 #9
At the risk of being burned at the stake...

Step 1. Add Microsoft.VisualBasic runtime to your references.
Step 2. Code, like you did in VB6

string s = Strings.Chr(64).ToString()
char c = Strings.Chr(64)

INeedADip wrote:
'Asc' returns an Integer value representing the character code
corresponding to a character

'Chr' is the inverse and returns the character associated with the
specified character code

What are the equivilents in C#?

Jan 4 '06 #10

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

Similar topics

4
3126
by: Darren | last post by:
Hi. I have a javascript menu system which uses the "elementFromPoint" function. However Netscape/Mozilla doesn't recognise this function. Does it have an equivilent? Thanks -- Darren
2
21732
by: John Galt | last post by:
I was looking at a VB.NET program that is passing strings to method calls using ByVal. I was wondering if this is for a reason and, if so, should a C# conversion of code samples from VB.NET also use an equivilent rather than passing strings by reference?
4
3560
by: trint | last post by:
I can't find the followings equivilent: flashPlayer.Movie = Application.StartupPath + @"/Cooler2.swf"; flashPlayer.FSCommand +=new AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEventHandler(flashPlayer_FSCommand); Any help is appreciated. Thanks, Trint
2
1498
by: Steve1 via DotNetMonster.com | last post by:
Hi all, I'm producing a data conversion app in C#. I have the code already written in VB6. I would to know if anyone knows of any links that shows the VB6 function/syntax equivilent in C#? For example in the VB6 code it has 'While rsSource.EOF' and 'Set rsTarget = ddTarget.OpenRecordset("SELECT * ..... etc...etc' , is there any links...
1
4853
by: Ryan Ternier | last post by:
I'm trying to find an equivilent function to VB's DatePart function. I have a Year, Month, the Day of week (Sunday, monday etc.) and the week (week 1, week 2) and I need to find the date of that month. Can anyone help me with this? /RT
4
6459
by: MikeLing | last post by:
Hi, String.IndexOf is case sensitive so I need to covert to lower before testing the string contains a given string. In VB this is: Instr(Lcase(StringToTest, "StringToCheckFor")) What is the C# equivilent? The closest I've comeup with is: string lowerString = testString.ToLower();
3
1362
by: Abubakar | last post by:
Hi, I have used the Monitor/manualresetevent class in .net to sycnhronize threads in C#, right now I'm programming in pure c++ and win32 and want to acheive the same functionality. Whats the equivilent of that in c++? int m_regards; _Ab_.
2
13292
by: Brian Henry | last post by:
Is there a .NET equivilent of the "internal" keyword in VB.NET? say if i had this in C# internal bool b_MyBoolean;
2
1678
by: =?Utf-8?B?QW5kcmV3IEhheWVz?= | last post by:
I have some legacy ASP code that looks like this: <% if not (Rs.EOF) then i=0 do while not Rs.EOF i=i+1 PaymentItemName = Rs("PaymentItemName") PaymentItemCode = Rs("PaymentItemCode") %>
3
2058
daniel aristidou
by: daniel aristidou | last post by:
Hi guys is there an equivilent of try (vb08) in vb6 If you dont know what try does in vb08 ... it tries to do something but if there is an error it simply ignores it. Try also contains commands like catch and finally. It solves many problems of programming for vb08 so i wondered if it did exist in vb6 cuase i cant seem to find it. If vb6 had...
0
7338
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...
0
7264
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7502
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. ...
0
7667
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7252
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7612
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...
1
5193
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...
0
3328
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
565
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.