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#
// 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
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#
// 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
Recommended Posts:
- C# | Byte.CompareTo(Byte) Method
- C# | Byte.Equals(Byte) Method
- C# | Converting an array of one type to an array of another type
- Converting Enumerated type to String according to the Specified Format in C#
- C# Program for Converting Hexadecimal String to Integer
- Program to convert Byte array to IP Address
- C# | BitConverter.ToString(Byte[]) Method
- C# | Byte.GetHashCode() Method
- C# | Byte.GetTypeCode() Method
- C# | Byte.ToString Method | Set - 1
- Byte Struct in C#
- C# | Byte.CompareTo(Object) Method
- C# | Byte.Equals(Object) Method
- C# | Byte.MinValue Field
- C# | Byte.MaxValue Field
- Difference between byte and sbyte in C#
- How to Convert ASCII Char to Byte in C#?
- byte Keyword in C#
- File.Replace(String, String, String) Method in C# with Examples
- File.Replace(String, String, String, Boolean) Method in C# with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

