c# convert byte array to string code example

Example 1: c# string to byte array

string author = "Mahesh Chand";  
// Convert a C# string to a byte array  
byte[] bytes = Encoding.ASCII.GetBytes(author);  

// Convert a byte array to a C# string. 
string str = Encoding.ASCII.GetString(bytes);

Example 2: c# store byte array as string

public static void Main()
    {
        byte[] bytes = Encoding.Default.GetBytes("ABC123");
        Console.WriteLine("Byte Array is: " + String.Join(" ", bytes));
 
        string str = Encoding.Default.GetString(bytes);
        Console.WriteLine("The String is: " + str);
    }

Example 3: c# bytes to string

string result = System.Text.Encoding.UTF8.GetString(byteArray);

Example 4: string from byte array c#

var str = System.Text.Encoding.Default.GetString(result);

Example 5: c# store byte array as string

public static void Main()
    {
        byte[] bytes = Convert.FromBase64String("QUJDMTIz");
        Console.WriteLine("Byte Array is: " + String.Join(" ", bytes));
 
        string str = Convert.ToBase64String(bytes);
        Console.WriteLine("The String is: " + str);
    }

Example 6: c# store byte array as string

string bitString = BitConverter.ToString(bytes);