VB icon

C# Chr and Asc Functions

Email
Submitted on: 1/3/2015 3:48:00 PM
By: Darrell Sparti, MCSD (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: C#
Views: 5539
 
     C# lacks VB's Chr and Asc functions. Unfortunately, none of the C# implementations I've found actually create the undocumented VB functions. Here are the true implentation of those functions. Note that simply casting a value as a char does not work for the entire range of characters. To see what I mean, create a test program in VB and one in C#. Do a simple loop from 1 to 255 and print out the characters using VB's Chr functions and simple casting in C#. Even using the ASCII encoding won't work as one submission here clains. If you don't want to bother looping and comparing 255 characters, try just using 137 for a value. You'll soon see what I mean. VB's Chr function will return one value but casting or using the ASCII encoding in C# will return a different value. Not cool if you're writing you own encryption algorithm or other code where a VB object must communicate with a C# object. So here's the real, undocumented deal, that will save you many hours of frustration!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: C# Chr and Asc Functions
// Description:C# lacks VB's Chr and Asc functions. Unfortunately, none of the C# implementations I've found actually create the undocumented VB functions. Here are the true implentation of those functions. Note that simply casting a value as a char does not work for the entire range of characters. To see what I mean, create a test program in VB and one in C#. Do a simple loop from 1 to 255 and print out the characters using VB's Chr functions and simple casting in C#. Even using the ASCII encoding won't work as one submission here clains. If you don't want to bother looping and comparing 255 characters, try just using 137 for a value. You'll soon see what I mean. VB's Chr function will return one value but casting or using the ASCII encoding in C# will return a different value. Not cool if you're writing you own encryption algorithm or other code where a VB object must communicate with a C# object. So here's the real, undocumented deal, that will save you many hours of frustration!
// By: Darrell Sparti, MCSD (from psc cd)
//
// Inputs:A single string character (for the Asc function) or an integer (for the Chr function)
//
// Returns:An integer (for the Asc function) or a single character (for the Chr function)
//
// Assumes:Add clsVB to your project. The methods are static so you can call them like this: myChar = clsVB.Chr(137) or myInt = clsVB.Asc("B")
//**************************************

internal class clsVB
{	
internal static string Chr(int p_intByte)
{
 if( (p_intByte < 0) || (p_intByte > 255) )
 {
 throw new ArgumentOutOfRangeException("p_intByte", p_intByte, "Must be between 1 and 255.");
 }
 byte[] bytBuffer = new byte[]{(byte) p_intByte};
 return Encoding.GetEncoding(1252).GetString(bytBuffer);
}
internal static int Asc(string p_strChar)
{
 if( (p_strChar.Length == 0) || (p_strChar.Length > 1) )
 {
 throw new ArgumentOutOfRangeException("p_strChar", p_strChar, "Must be a single character.");
 }
 char[] chrBuffer = {Convert.ToChar(p_strChar)}; 
 byte[] bytBuffer = Encoding.GetEncoding(1252).GetBytes(chrBuffer); 
 return (int) bytBuffer[0];
}
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.