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!
3 comments on »Convert Byte Array to String in C#«
is the “0” index of a byte in array or the whole array is converted to string in this last line? what is exactly 0 in that line?
Thats just a start index of the byte array.
var convertedStr = BitConverter.ToString(ByteArray);
BitConverter also works well