misc.

This snippet is for miscellaneous bits of code specific to individual languages.

Download misc.zip

Synopsis:

misc.cs


misc.cs

Synopsis
using System;
using ut;

//This class is for bits and pieces specific to csharp
class testmisc
{
  public void testCharByteConversions()
  {
    byte b = 0x30;
    utx.assert(Convert.ToChar(b), '0'); 
    char c = '0';
    utx.assert(Convert.ToByte(c), (byte) 0x30); 
  }
  
  // Since a char is not necessarily a single byte,
  // there is no String to Byte[] conversion
  byte[] StringToByteArray(string s)
  {
    char[] schars = s.ToCharArray();
    byte[] sbytes = new byte[s.Length];
    for(int i = 0; i < s.Length; ++i)
      sbytes[i] = Convert.ToByte(schars[i]);
    return sbytes;
  }

  string ByteArrayToString(byte[] sbytes)
  {
    string str = "";
    foreach(byte b in sbytes)
    {
      if (b == 0) break;
      str += Convert.ToChar(b);
    }
    return str;
  }

  public void testCharByteArrayConversions()
  {
    utx.assert(ByteArrayToString(StringToByteArray("")), "");
    utx.assert(ByteArrayToString(StringToByteArray("abc")), "abc"); 
    utx.assert(ByteArrayToString(StringToByteArray("\n")), "\n"); 
  }
}






Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001-2010