Returns a text representation of a byte array. Bytes in the array which don't convert to printable ascii characters are dropped. - CSharp System

CSharp examples for System:Byte Array

Description

Returns a text representation of a byte array. Bytes in the array which don't convert to printable ascii characters are dropped.

Demo Code

/// Distributed under the Mozilla Public License                            *
using System;/*  w  ww  .  ja v  a2 s  .  com*/

public class Main{
        /// <summary> Returns a text representation of a byte array.
      /// Bytes in the array which don't convert to printable ascii characters
      /// are dropped.
      /// *
      /// </summary>
      /// <param name="bytes">a byte array
      /// </param>
      /// <returns> a string containing the ascii equivalent of the bytes.
      /// 
      /// </returns>
      public static System.String toString(byte[] bytes)
      {
         System.IO.StringWriter sw = new System.IO.StringWriter();
         
         int length = bytes.Length;
         if (length > 0)
         {
            for (int i = 0; i < length; i++)
            {
               byte b = bytes[i];
               if (b > 32 && b < 127)
                  sw.Write((char) b);
            }
         }
         return (sw.ToString());
      }
}

Related Tutorials