November 21, 2005

Helper Class To Converts Strings To Ascii and Back

using System.Text;

...

// Helper methods for converting strings to ascii and back
public class Ascii
{
 // Converts a standard c# unicode string to an array of ASCII characters
 public static byte[] FromString(string str)
 {
  Encoding ascii = Encoding.ASCII;
  byte[] asciiBytes = ascii.GetBytes(str);
  return asciiBytes;
 }

 // Converts an array of ASCII characters to a standard c# unicode string 
 public static string ToString(byte[] asciiString)
 {
  // Create two different encodings.
  Encoding ascii = Encoding.ASCII;
  string str = ascii.GetString(asciiString);
  return str;
 }
}

No comments: