Converting a String to its Equivalent Byte Array in C#

28 May, 2020
Given a string, the task is to convert this string into a Byte array in C#.
Examples:
Input: aA
Output: [97, 65 ]
  
Input: Hello
Output: [ 72, 101, 108, 108, 111 ]

Using ToByte() Method: This method is a Convert class method. It is used to converts other base data types to a byte data type.
Syntax:
byte byt = Convert.ToByte(char); 

Step 1: Get the string.
Step 2: Create a byte array of the same length as of string.
Step 3: Traverse over the string to convert each character into byte using the ToByte() Method and store all the bytes to the byte array.
Step 4: Return or perform the operation on the byte array.
Below is the implementation of the above approach:
C#
// C# program to convert a given// string to its equivalent byte[]    using System;  public class GFG{        static public void Main ()    {         string str = "GeeksForGeeks";             // Creating byte array of string length         byte[] byt = new byte[str.Length];             // converting each character into byte         // and store it        for (int i = 0; i < str.Length; i++) {             byt[i] = Convert.ToByte(str[i]);         }             // printing characters with byte values        for(int i =0; i<byt.Length; i++)        {            Console.WriteLine("Byte of char \'" + str[i] + "\' : " + byt[i]);        }      } }
Output:
Byte of char 'G' : 71
Byte of char 'e' : 101
Byte of char 'e' : 101
Byte of char 'k' : 107
Byte of char 's' : 115
Byte of char 'F' : 70
Byte of char 'o' : 111
Byte of char 'r' : 114
Byte of char 'G' : 71
Byte of char 'e' : 101
Byte of char 'e' : 101
Byte of char 'k' : 107
Byte of char 's' : 115

Using GetBytes() Method: The Encoding.ASCII.GetBytes() method is used to accepts a string as a parameter and get the byte array.
Syntax:
byte[] byte_array = Encoding.ASCII.GetBytes(string str); 

Step 1: Get the string.
Step 2: Create an empty byte array.
Step 3: Convert the string into byte[] using the GetBytes() Method and store all the convert string to the byte array.
Step 4: Return or perform the operation on the byte array.
C#
// C# program to convert a given// string to its equivalent byte[]    using System;using System.Text;  public class GFG{        static public void Main ()    {         string str = "GeeksForGeeks";             // Creating byte array of string length         byte[] byt;             // converting each character into byte         // and store it        byt = Encoding.ASCII.GetBytes(str);            // printing characters with byte values        for(int i =0; i<byt.Length; i++)        {            Console.WriteLine("Byte of char \'" + str[i] + "\' : " + byt[i]);        }      } }
Output:
Byte of char 'G' : 71
Byte of char 'e' : 101
Byte of char 'e' : 101
Byte of char 'k' : 107
Byte of char 's' : 115
Byte of char 'F' : 70
Byte of char 'o' : 111
Byte of char 'r' : 114
Byte of char 'G' : 71
Byte of char 'e' : 101
Byte of char 'e' : 101
Byte of char 'k' : 107
Byte of char 's' : 115

CSharp-Byte-Struct
CSharp-string
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Multiple inheritance using interfaces
Introduction to .NET Framework
C# | Delegates
Differences Between .NET Core and .NET Framework
C# | Data Types
C# | String.IndexOf( ) Method | Set - 1
C# | Replace() Method
C# | Arrays
Extension Method in C#
C# | List Class