How to Convert String To Byte Array in C#

The encoding functionality in .NET is defined in the System.Text.Encoding. The Encoding class is an abstract class. That means you can use its functionality via its derived classes only. 

The Encoding.GetBytes() method converts a string into a bytes array in C#.

The following code example converts a C# string into a byte array in Ascii format and prints the converted bytes to the console

string author = "Mahesh Chand";  
// Convert a C# string to a byte array  
byte[] bytes = Encoding.ASCII.GetBytes(author);  
foreach ( byte b in bytes)  
{  
   Console.WriteLine(b);  
}

The Encoding.GetString() method converts an array of bytes into a string. The following code snippet converts an ASCII byte array into a string and prints the converted string to the console. 

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

What if we need to convert one encoding format to another? The Encoding class provides the functionality to convert from one encoding to another, i.e., conversion from ASCII to Unicode. 

The Encoding.Covert() method converts a range of bytes or an entire byte array in a byte array from one encoding to another. This code example demos conversion from Unicode to Ascii encoding format. 

// Convert one Encoding type to another  
string authorName = "Here is a unicode characters string. Pi (\u03a0)";  
// Create two different encodings.  
Encoding ascii = Encoding.ASCII;  
Encoding unicode = Encoding.Unicode;  
// Convert unicode string into a byte array.  
byte[] bytesInUni = unicode.GetBytes(authorName);  
// Convert unicode to ascii  
byte[] bytesInAscii = Encoding.Convert(unicode, ascii, bytesInUni);  
// Convert byte[] into a char[]  
char[] charsAscii = new char[ascii.GetCharCount(bytesInAscii, 0, bytesInAscii.Length)];  
ascii.GetChars(bytesInAscii, 0, bytesInAscii.Length, charsAscii, 0);  
// Convert char[] into a ascii string  
string asciiString = new string(charsAscii);

Here is the complete code example.

using System;  
using System.Text;  
namespace ConsoleApp1   
{  
    class Program   
    {  
        static void Main(string[] args)   
        {  
            string author = "Mahesh Chand";  
            // Convert a C# string to a byte array    
            byte[] bytes = Encoding.ASCII.GetBytes(author);  
            foreach(byte b in bytes) {  
                Console.WriteLine(b);  
            }  
            // Convert a byte array to a C# string    
            string str = Encoding.ASCII.GetString(bytes);  
            Console.WriteLine(str);  
            // Convert one Encoding type to another    
            string authorName = "Here is a unicode characters string. Pi (\u03a0)";  
            // Create two different encodings.    
            Encoding ascii = Encoding.ASCII;  
            Encoding unicode = Encoding.Unicode;  
            // Convert unicode string into a byte array.    
            byte[] bytesInUni = unicode.GetBytes(authorName);  
            // Convert unicode to ascii    
            byte[] bytesInAscii = Encoding.Convert(unicode, ascii, bytesInUni);  
            // Convert byte[] into a char[]    
            char[] charsAscii = new char[ascii.GetCharCount(bytesInAscii, 0, bytesInAscii.Length)];  
            ascii.GetChars(bytesInAscii, 0, bytesInAscii.Length, charsAscii, 0);  
            // Convert char[] into a ascii string    
            string asciiString = new string(charsAscii);  
            // Print unicode and ascii strings    
            Console.WriteLine($ "Author Name: {authorName}");  
            Console.WriteLine($ "Ascii converted name: {asciiString}");  
            Console.ReadKey();  
        }  
    }  
}

Summary

The code example shows the conversion of a string into a byte array using C#.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.