Encode a string into a byte array in C#
This post will discuss how to encode a string into a byte array in C#.
1. Using Encoding.GetBytes() method
The Encoding.GetBytes() method can be used to encode all the characters in the specified string into a byte array.
The following program creates a string extension method, ToByteArray(), which encodes the specified string into a sequence of bytes using specified ASCII encoding.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; using System.Text; public static class Extention { public static byte[] ToByteArray(this string str, Encoding encoding) { return encoding.GetBytes(str); } } public class Program { public static void Main() { Encoding encoding = Encoding.ASCII; string str = "ABC123"; byte[] bytes = str.ToByteArray(encoding); Console.WriteLine("Byte Array is " + String.Join(",", bytes)); } } /* Output: Byte Array is 65,66,67,49,50,51 */ |
To decode the byte array back into a string, we can use the Encoding.GetString() method using the same ASCII encoding used for byte array conversion.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Text; public class Program { public static string ByteArrayToString(byte[] bytes, Encoding encoding) { return encoding.GetString(bytes); } public static void Main() { Encoding encoding = Encoding.ASCII; byte[] bytes = encoding.GetBytes("ABC123"); string str = ByteArrayToString(bytes, encoding); Console.WriteLine(str); } } /* Output: ABC123 */ |
2. Using Buffer.BlockCopy() method
Here, the idea is to convert the given string into a character array and then use the Buffer.BlockCopy() method to copy all bytes from the character array to an empty byte array. The encoding used is Unicode since the ToCharArray() method returns a Unicode character array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; public class Example { public static byte[] ToByteArray(string str) { byte[] bytes = new byte[str.Length * sizeof(char)]; // Unicode endoding Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); return bytes; } public static void Main() { string str = "ABC123"; byte[] bytes = ToByteArray(str); Console.WriteLine("Byte Array is " + String.Join(",", bytes)); } } /* Byte Array is 65,0,66,0,67,0,49,0,50,0,51,0 */ |
To decode the byte array back into a string, we can use the following method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; using System.Text; public class Program { public static string ByteArrayToString(byte[] bytes) { char[] chars = new char[bytes.Length / sizeof(char)]; Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length); return new string(chars); } public static void Main() { byte[] bytes = Encoding.Unicode.GetBytes("ABC123"); string decoded = ByteArrayToString(bytes); Console.WriteLine(decoded); } } /* Output: ABC123 */ |
That’s all about encoding a string into a byte array in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)