Converting an array of bytes to a string. - CSharp Language Basics

CSharp examples for Language Basics:Data Type Cast

Description

Converting an array of bytes to a string.

Demo Code

using System;/*  www.  j a v a  2  s . c  om*/
using System.Text;                // Need for type UTF8Encoding.
using System.Globalization;       // Need for cultures.
class Program
{
   static void Main(string[] args)
   {
      // Converting an array of bytes to a string.
      Console.WriteLine("\nUse an 'encoding' to convert an array " + "of bytes to a string.");
      byte[] bytes = { 67, 68, 109, 32, 69, 73, 70, 32,73, 72, 104, 97, 114 };
      UTF8Encoding encoding = new UTF8Encoding();
      string name = encoding.GetString(bytes, 0, bytes.Length);
      Console.WriteLine(name);
   }
}

Result


Related Tutorials