Skip to content
Related Articles

Related Articles

Converting a String to its Equivalent Byte Array in C#
  • Last Updated : 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 ]

Method 1: 

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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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]);
        }
  
    
}

chevron_right


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

Method 2: 

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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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]);
        }
  
    
}

chevron_right


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
My Personal Notes arrow_drop_up
Recommended Articles
Page :