Outputs a UTF8 string converted from a subset of bytes in this array. - CSharp System

CSharp examples for System:Byte Array

Description

Outputs a UTF8 string converted from a subset of bytes in this array.

Demo Code

//  Licensed under the Apache License, Version 2.0 (the "License");
using System.Collections;
using System.Text;
using System;/* ww w.  j a  v  a2 s.co m*/

public class Main{
    /// <summary>
      /// Outputs a UTF8 string converted from a subset of bytes in this array.
      /// </summary>
      /// <param name="offset">Start index</param>
      /// <param name="length">Number of bytes to convert.</param>
      /// <returns>A string converted from bytes in this array.</returns>
      public static string ToString(this byte[] array, int offset, int length)
      {
         if (array == null || array.Length == 0 || array.Length < (offset + length))
            return String.Empty;

         return new string(Encoding.UTF8.GetChars(array, offset, length));
      }
      /// <summary>
      /// Outputs a UTF8 string converted from all bytes in this array.
      /// </summary>
      /// <param name="offset">Start index</param>
      /// <param name="length">Number of bytes to process</param>
      /// <returns>A string converted from all bytes in this array.</returns>
      public static string ToString(this byte[] array)
      {
         if (array == null || array.Length == 0)
            return String.Empty;

         return new string(Encoding.UTF8.GetChars(array));
      }
}

Related Tutorials