Advertisement
7_2009-2012 Debugging and Error Handling #217416

C# Chr and Asc Functions

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!

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
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];
  }
}
Original Comments (3)
Recovered from Wayback Machine