xxxxxxxxxx
public static byte[] StringToByteArray(string hex) {
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
xxxxxxxxxx
string author = "Mahesh Chand";
// Convert a C# string to a byte array
byte[] bytes = Encoding.ASCII.GetBytes(author);
// Convert a byte array to a C# string.
string str = Encoding.ASCII.GetString(bytes);
xxxxxxxxxx
// Convert a string to a C# byte[]
//change encoding depending on your data
string someText = "some data as text.";
byte[] bytes = Encoding.ASCII.GetBytes(author);
// Convert a byte array to a C# string
string str = Encoding.ASCII.GetString(bytes);
Console.WriteLine(str);
convert a string to a byte array in C# using the Encoding class.
xxxxxxxxxx
string str = "Hello World";
byte[] byteArray = Encoding.UTF8.GetBytes(str);
the GetBytes method of the Encoding class is used to convert the string to a byte array using the UTF-8 encoding. You can use other encodings, such as ASCII or Unicode, depending on your needs.
Note that the resulting byte array will contain one byte for each character in the string, encoded according to the specified encoding.
xxxxxxxxxx
string input = "Hello, World!";
byte[] byteArray = Encoding.UTF8.GetBytes(input);
xxxxxxxxxx
using System.Text;
public static byte[] encode(string stringToEncode)
{
UTF8Encoding utf8 = new UtF8Encoding();
byte[] bytename = new byte[1024];
bytename = utf8.GetBytes(stringToEncode);
return bytename;
}