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

CSharp examples for System:Byte Array

Description

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

Demo Code


using System.Text;
using System.Collections;
using System;/* www  .  j  av  a  2 s .com*/

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(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(String sourceString)
    {
      return 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