How to Convert Byte Array to String in C#

In .NET, a byte is just a number from 0 to 255 (the numbers that can be represented by eight bits). So, a byte array is just an array of the numbers from 0 to255. At a lower level, an array is a contiguous block of memory, and a byte array is just a representation of that memory in 8-bit blocks.

Let's say you have a Byte[] array loaded from a file and you need to convert it to a String.

1. Encoding's GetString
but you won't be able to get the original bytes back if those bytes have non-ASCII characters

byte[] bytes = { 130, 200, 234, 23 }; // A byte array contains non-ASCII (or non-readable) characters
string Enco = Encoding.UTF8.GetString(bytes); 
byte[] decBytes1 = Encoding.UTF8.GetBytes(Enco);  // decBytes1.Length == 10 !!
// decBytes1 not same as bytes
// Using UTF-8 or other Encoding object will get similar results

2. BitConverter.ToString
The output is a "-" delimited string, but there's no .NET built-in method to convert the string back   to byte array.

string Bitconvo = BitConverter.ToString(bytes);   // 82-C8-EA-17
String[] tempAry = Bitconvo.Split('-');
byte[] decBytes2 = new byte[tempAry.Length];
for (int i = 0; i < tempAry.Length; i++)
    decBytes2[i] = Convert.ToByte(tempAry[i], 16);
// decBytes2 same as bytes

3. Convert.ToBase64String
You can easily convert the output string back to byte array by using Convert.FromBase64String.
Note: The output string could contain '+', '/' and '='. If you want to use the string in a URL, you need to explicitly encode it.

string B64 = Convert.ToBase64String(bytes);  
byte[] decByte3 = Convert.FromBase64String(B64);
// decByte3 same as bytes

4. HttpServerUtility.UrlTokenEncode
You can easily convert the output string back to byte array by using HttpServerUtility.UrlTokenDecode. The output string is already URL friendly! The downside is it needs System.Web assembly if your project is not a web project.

string s3 = Convert.ToBase64String(bytes);  // gsjqFw==
byte[] decByte3 = Convert.FromBase64String(s3);
// decByte3 same as bytes

Credits : combo_ci

28 comments

Thank you for sharing Its a very informative post.
Best IT Training in Bangalore

Reply

I just want to tell you that Iím all new to blogging and site-building and really loved your web page. Very likely Iím want to bookmark your site . You amazingly have remarkable posts. Thanks a bunch for sharing your blog.
Regards - www.office.com/setup
www.office.com/setup

Reply

the information which you have provided is better then other blog.
so nice work keep it up. And thanks for sharing.
Anonymous Hosting

Reply

You got an extremely helpful website I actually have been here reading for regarding an hour. I’m an initiate and your success is incredibly a lot of a concept on behalf of me.
python training in chennai
python training in Bangalore
Python training institute in chennai

Reply

I would really like to read some personal experiences like the way, you've explained through the above article. I'm glad for your achievements and would probably like to see much more in the near future. Thanks for share.angularjs online Training

angularjs Training in marathahalli

angularjs interview questions and answers

angularjs Training in bangalore

angularjs Training in bangalore

angularjs Training in chennai

Reply

Your blog was really amazing and informative. Thanks for sharing with us. Visit for
Web Designing Company in Delhi
SEO Service in Delhi

Reply

Thanks for given such informative and valuable blog. Visit for
SEO Service in Delhi

Reply

Brilliant ideas that you have share with us.It is really help me lot and i hope it will help others also.update more different ideas with us.
devops Training in Nolambur
devops Training in Perambur
devops Training in Mogappair
devops Training in Thirumangalam

Reply

Great. Having valuable information which help for youngster to understand clearly about C#.
java coaching centers in bangalore

Reply

This is best one article so far I have read online, I would like to appreciate you for making it very simple and easy
Regards,
Data Science Certification Training

Reply

Good Information and thanks for this great content. Keep Sharing

Hadoop Training in Bangalore

Reply

Good Post with really useful content, thanks for sharing with us.

C++ Training in Bangalore | IT Training in Bangalore

Reply

You are doing a great job. I would like to appreciate your work for good accuracy

Machine Learning in Chennai

Reply

And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
SOFTWARE TRAINING IN CHENNAI
POWERBI TRAINING IN CHENNAI

Reply

Post a Comment

Note: Only a member of this blog may post a comment.