C# String To Byte Array

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 following is the list of Encoding derived classes and their purpose. 
  1. ASCIIEncoding: Encodes Unicode characters as single 7-bit ASCII characters.
  2. UTF7Encoding encodes Unicode characters using the UTF-7 encoding.
  3. UTF8Encoding encodes Unicode characters using the UTF-8 encoding.
  4. UnicodeEncoding encodes Unicode characters using the UTF-16 encoding.
  5. UTF32Encoding encodes Unicode characters using the UTF-32 encoding. 
The Encoding.GetBytes() method converts a string into a bytes array. The following code example converts a C# string into a byte array in Ascii format and prints the converted bytes to the console. 
  1. string author = "Mahesh Chand";  
  2. // Convert a C# string to a byte array  
  3. byte[] bytes = Encoding.ASCII.GetBytes(author);  
  4. foreach ( byte b in bytes)  
  5. {  
  6.    Console.WriteLine(b);  
  7. }  
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. 
  1. // Convert a byte array to a C# string. 
  2. string str = Encoding.ASCII.GetString(bytes);  
  3. 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. 
  1. // Convert one Encoding type to another  
  2. string authorName = "Here is a unicode characters string. Pi (\u03a0)";  
  3. // Create two different encodings.  
  4. Encoding ascii = Encoding.ASCII;  
  5. Encoding unicode = Encoding.Unicode;  
  6. // Convert unicode string into a byte array.  
  7. byte[] bytesInUni = unicode.GetBytes(authorName);  
  8. // Convert unicode to ascii  
  9. byte[] bytesInAscii = Encoding.Convert(unicode, ascii, bytesInUni);  
  10. // Convert byte[] into a char[]  
  11. char[] charsAscii = new char[ascii.GetCharCount(bytesInAscii, 0, bytesInAscii.Length)];  
  12. ascii.GetChars(bytesInAscii, 0, bytesInAscii.Length, charsAscii, 0);  
  13. // Convert char[] into a ascii string  
  14. string asciiString = new string(charsAscii);  
Here is the complete code example.
  1. using System;  
  2. using System.Text;  
  3. namespace ConsoleApp1   
  4. {  
  5.     class Program   
  6.     {  
  7.         static void Main(string[] args)   
  8.         {  
  9.             string author = "Mahesh Chand";  
  10.             // Convert a C# string to a byte array    
  11.             byte[] bytes = Encoding.ASCII.GetBytes(author);  
  12.             foreach(byte b in bytes) {  
  13.                 Console.WriteLine(b);  
  14.             }  
  15.             // Convert a byte array to a C# string    
  16.             string str = Encoding.ASCII.GetString(bytes);  
  17.             Console.WriteLine(str);  
  18.             // Convert one Encoding type to another    
  19.             string authorName = "Here is a unicode characters string. Pi (\u03a0)";  
  20.             // Create two different encodings.    
  21.             Encoding ascii = Encoding.ASCII;  
  22.             Encoding unicode = Encoding.Unicode;  
  23.             // Convert unicode string into a byte array.    
  24.             byte[] bytesInUni = unicode.GetBytes(authorName);  
  25.             // Convert unicode to ascii    
  26.             byte[] bytesInAscii = Encoding.Convert(unicode, ascii, bytesInUni);  
  27.             // Convert byte[] into a char[]    
  28.             char[] charsAscii = new char[ascii.GetCharCount(bytesInAscii, 0, bytesInAscii.Length)];  
  29.             ascii.GetChars(bytesInAscii, 0, bytesInAscii.Length, charsAscii, 0);  
  30.             // Convert char[] into a ascii string    
  31.             string asciiString = new string(charsAscii);  
  32.             // Print unicode and ascii strings    
  33.             Console.WriteLine($ "Author Name: {authorName}");  
  34.             Console.WriteLine($ "Ascii converted name: {asciiString}");  
  35.             Console.ReadKey();  
  36.         }  
  37.     }  
  38. }   
The code example shows the conversion of a string into a byte array using C#.

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.