Converts an array of object-type instances to a byte-type array. - CSharp System

CSharp examples for System:Byte Array

Description

Converts an array of object-type instances to a byte-type array.

Demo Code


using System;//from   w w  w.j av  a  2s.  co  m

public class Main{
        /// <summary>
   /// Converts a array of object-type instances to a byte-type array.
   /// </summary>
   /// <param name="tempObjectArray">Array to convert.</param>
   /// <returns>An array of byte type elements.</returns>
   public static byte[] ToByteArray(System.Object[] tempObjectArray)
   {
      byte[] byteArray = null;
      if (tempObjectArray != null)
      {
         byteArray = new byte[tempObjectArray.Length];
         for (int index = 0; index < tempObjectArray.Length; index++)
            byteArray[index] = (byte)tempObjectArray[index];
      }
      return byteArray;
   }
        /// <summary>
   /// Converts a string to an array of bytes
   /// </summary>
   /// <param name="sourceString">The string to be converted</param>
   /// <returns>The new array of bytes</returns>
   public static byte[] ToByteArray(System.String sourceString)
   {
      return System.Text.UTF8Encoding.UTF8.GetBytes(sourceString);
   }
        /// <summary>
   /// Converts an array of sbytes to an array of bytes
   /// </summary>
   /// <param name="sbyteArray">The array of sbytes to be converted</param>
   /// <returns>The new array of bytes</returns>
   public static byte[] ToByteArray(sbyte[] sbyteArray)
   {
      byte[] byteArray = null;

      if (sbyteArray != null)
      {
         byteArray = new byte[sbyteArray.Length];
         for(int index=0; index < sbyteArray.Length; index++)
            byteArray[index] = (byte) sbyteArray[index];
      }
      return byteArray;
   }
}

Related Tutorials