Convert Byte Array to String in C#

Published on March 24, 2015 by

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!

Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

3 comments on »Convert Byte Array to String in C#«

  1. Slon

    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?

  2. Vincent

    @Slon
    Thats just a start index of the byte array.

  3. Vikas

    var convertedStr = BitConverter.ToString(ByteArray);
    BitConverter also works well

Leave a Reply

Your e-mail address will not be published.