Convert String to/from Byte Array C# Examples

This tutorial covers working with strings and byte arrays in C#.

  • How to parse a string to a byte array.
  • How to convert a byte array to a string in C#.

String is an inbuilt type that stores a sequence of characters. Byte Array is an array of bytes used to store binary data. There is no direct way to convert one type to another; manual conversion is required.

Convert String to Byte Array in C# with examples

There are multiple ways to convert a string to a byte array.

  • Using Encoding.ASCII.GetBytes()

    This approach works if the string contains ASCII characters. The default encoding is ASCII, not UTF-16.

    ASCIIuses 7 bits to represent characters and allows limited character sets, Whereas UTF-16 uses 16 bits to represent characters and accommodate large character sets.

    if the string contains non-ASCII characters, it is replaced with ? while converting to a byte array.

    First, use the System.Text namespace in your code

    byte[] bytes = Encoding.ASCII.GetBytes("Hello");
    

    Here is an example

    using System;
    using System.Text;
    public class Program {
      public static void Main() {
          byte[] bytes = Encoding.ASCII.GetBytes("Hello");
          string str = Encoding.ASCII.GetString(bytes);
          Console.WriteLine(str);
    
      }
    }
    

    Another example is if the string contains UTF-16 characters

    using System;
    using System.Text;
    public class Program {
      public static void Main() {
          byte[] bytes = Encoding.ASCII.GetBytes("Hello�");
          string str = Encoding.ASCII.GetString(bytes); //sHello?
          Console.WriteLine(str);
    
      }
    }
    

    The above example contains a string of , which is a UTF-16 character, Output contains a character ?

  • Use Encoding.UTF8.GetBytes() It is easy to convert a string to a byte array using UTF-8 encoding. UTF-8 is an 8-bit encoding used widely for encoding text

    First, Install the System.Memory package using Nuget. It provides ReadOnlySpan to read the memory bytes.

    using System;
    using System.Text;
    public class Program {
      public static void Main() {
            // Convert  string to  byte array
          ReadOnlySpan<byte> bytes1 = System.Text.Encoding.UTF8.GetBytes("hello1");
    
          // Convert byte array to string
          string str = Encoding.ASCII.GetString(bytes1);//hello1
    
          Console.WriteLine(str);
    
      }
    }
    
  • Using u8 String Literal Syntax (C# 11):

    The C# 11 version, uses UTF-8 string literals syntax.

    Append u8 to a string converted into a byte array.

    using System;
    using System.Text;
    public class Program {
      public static void Main() {
        // Convert  string to  byte array
        ReadOnlySpan<byte> bytes2 = "hello3"u8;
        // Convert byte array to string
        string str = Encoding.ASCII.GetString(bytes2); //hello3
    
        Console.WriteLine(str);
    
      }
    }
    
  • Assign the string variable to a byte array

    The fourth way, assign the string variable to a byte array, It casts and converts to the array.

    string str = "welcome";
    byte[] array = str;
    
  • Use Encoding.Unicode.GetBytes

    This method is used to encode a text with UTF-16 encoding,UTF-16 is a 16-bit encoding to represent each character in a large character set.

    string str = "welcome";
    byte[] array = Encoding.Unicode.GetBytes(str);
    
    
  • Use Encoding.UTF32.GetBytes

    This method is used to encode a text with UTF-32 encoding,

    UTF-32 is a 32-bit encoding to represent each character, supports a wide range of characters

    string str = "welcome";
    byte[] array = Encoding.UTF32.GetBytes(str);
    
    

Byte Array to String in C# with examples

Multiple ways to convert byte array to String

  • Using Encoding.ASCII.GetString

    string str = Encoding.ASCII.GetString(bytes); // hello
    

    It supports ASCII, but not UTF-8 or UTF-16 encoding.

    The second way, use System.Text.Encoding.UTF8.GetString

    Takes byte array and return string

    using System;
    using System.Text;
    public class Program {
      public static void Main() {
          byte[] bytes = Encoding.ASCII.GetBytes("Hello");
    
          ReadOnlySpan<byte> bytes1 = System.Text.Encoding.UTF8.GetBytes("hello1");
          string str = System.Text.Encoding.UTF8.GetString(bytes1);
    
          Console.WriteLine(str); // hello1
    
      }
    }