Convert Byte Array to String in C#

Converting a byte array to a stringĀ in C# is easy. In fact, it can be done in a single line. Below is an example that converts a string into a byte array. In the example that follows, we will then convert that byte array back to a string, effectively showing you how to do the conversion both ways.

string stringToConvert = "This will be converted into a byte array and back";
byte[] buffer = Encoding.UTF8.GetBytes(stringToConvert);

Now if we want to do the conversion in the other direction, that is from a byte array to a string, then this can simply be done as follows.

string converted = Encoding.UTF8.GetString(buffer, 0, buffer.Length);

As you can see, C# makes it very easy to convert strings to and from byte arrays. I hope this helped you. Thank you for reading!

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *