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#
// 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
Similar Reads
Convert a Character to the String in C# Given a character, the task is to character to the string in C#. Examples: Input : X = 'a' Output : string S = "a" Input : X = 'A' Output : string S = "A" Approach: The idea is to use ToString() method, the argument is the character and it returns string converting Unicode character to the string. /
1 min read
C# | Copying BitArray elements to an Array The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.CopyTo(Array, Int32) method is used to copy the ent
3 min read
Program to convert Byte array to IP Address Given a Byte Array, convert it to the format of IP Address. Examples: Input : {16, 16, 16, 16} Output : 16.16.16.16Input : {172, 31, 102, 14} Output : 172.31.102.14Byte arrays: A byte is a collection of bits (8). Byte arrays are arrays of contiguous bytes and can be used to store binary information.
3 min read
C# | Number of elements contained in the BitArray The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.Count property is used to get the number of element
2 min read
C# | Convert.ToByte(String, IFormatProvider) Method This method is used to convert the specified string representation of a number to an equivalent 8-bit unsigned integer, using specified culture-specific formatting information.Syntax: public static byte ToByte (string value, IFormatProvider provider); Parameters: value: It is a string that contains
4 min read