How to convert byte array to string in c#

This is a very simple code showing how to convert byte array to string in c#

Make sure that you have imported the namespace System.Text

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Blog_CS
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] bytes = GetBytes("the quick brown fox jump over the lazy dog");
            //Convert byte array to string.
            string message = Encoding.Default.GetString(bytes);
            Console.WriteLine(message);
        }

        private static byte[] GetBytes(string message)
        {
            //Make sure that you have imported 'System.Text' namespace
            return Encoding.Default.GetBytes(message);
        }

    }
}

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads

CodeGuru