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

39 comments

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

Reply

CIITN is the best SAP training and placement Institute in noida. Contestant will analyze in keeping with modern-day enterprise trends by way of certified and industry experts & SAP training Institute in Noida. They provide a 100% placement help with the best training of IT cousrses.

If you are looking best sap training in Noida then you can join CIITN. One of the premier training and development institute for SAP Training Program in Noida. The company provides 6 weeks/ 6 Months Training for SAP. They provide training in all SAP Modules like SAP HANA,SAP B1, SAP FICO etc. Just come for free demo class and then join SAP and move up in your career .

SAP Training Institute in Noida
sap training in noida
sap institute in noida
sap course in noida
best sap training institute in noida

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 knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.

ccna training in chennai



ccna training in bangalore


ccna training in pune

Reply

Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.
Best AWS Training in Chennai | Amazon Web Services Training in Chennai

AWS Training in Bangalore | Amazon Web Services Training in Bangalore

Amazon Web Services Training in Pune | Best AWS Training in Pune

Reply


Thank you very much for for your great information. It really makes me happy and I am satisfied with the arrangement of your post. You are really a talented person I have ever seen. I will keep following you forever. If you have time you can check this site and give me suggestion.
Hotel Bedding Supplies

Reply

hello sir,
thanks for giving that type of information.
digital marketing company in delhi

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

nice work keep it up thanks for sharing the knowledge.Thanks for sharing this type of information, it is so useful. Epoxy Grout manufacturer in delhi

Reply

I just see the post i am so happy the post of information's.So I have really enjoyed and reading your blogs for these posts.Any way I’ll be subscribing to your feed and I hope you post again soon.
best selenium training institute in hyderabad
best selenium online training institute in hyderabad
best institute for selenium training in hyderabad

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

Install Office Setup – Sign-in to you microsoft account and then Enter 25 digit alphanumeric office setup product key on country and language.click on next to start office installation.We are the best Office Setup in US, Canada and Australia. At Office Setup, we put high effort, moderate IT answers for organization's, and people.Whether set up or beginning, equipment or programming, system or electronic, we have something for each financial plan. officeplus.net is the exchanging name of Spacesolutions Pvt Ltd situated in California, USA. We are not Microsoft. However we are trained professionals to provide technical support for We are the world's biggest innovation wholesaler and a main innovation deals, showcasing and logistics organization for the IT business around the world. Our people group based ethos, focused costs and nature of administration settles on Office Setup the right decision for your IT needs .Whether you are a learner or experienced in IT our courses are altered to furnish you with the key achievement criteria to thrive in future attempts.
www.office.com/setup

Reply

The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
Devops Training in pune

Reply

Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

rpa interview questions and answers
automation anywhere interview questions and answers
blueprism interview questions and answers
uipath interview questions and answers
rpa training 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

Awesome post, continue sharing more like this.

Article submission sites
Education

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

Post a Comment