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
Very Informative Post.
ReplyBest Software Training institute in bangalore
Best SAP Training institute In Bangalore.
Best Advanced Excel Training Institute In Bangalore.
Thank you for sharing Its a very informative post.
ReplyBest IT Training in Bangalore
Very Informative Post.
ReplyBest Oracle Training in Bangalore
Nice Post. Thanks for the information.
ReplySEO learning center in Bangalore
Good post!
ReplyClick here.
Final year java projects chennai
Big data hadoop training in chennai
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.
ReplyRegards - www.office.com/setup
www.office.com/setup
the information which you have provided is better then other blog.
Replyso nice work keep it up. And thanks for sharing.
Anonymous Hosting
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.
Replypython training in chennai
python training in Bangalore
Python training institute in chennai
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
Replyangularjs Training in marathahalli
angularjs interview questions and answers
angularjs Training in bangalore
angularjs Training in bangalore
angularjs Training in chennai
digital marketing company in delhi
ReplyYour blog was really amazing and informative. Thanks for sharing with us. Visit for
ReplyWeb Designing Company in Delhi
SEO Service in Delhi
Informative post, thanks for sharing.
Replyccna Training in Chennai
ccna course in Chennai
ccna Training institute in Chennai
RPA Training in Chennai
RPA courses in Chennai
Blue Prism Training in Chennai
UiPath Training in Chennai
I am really enjoying reading your well written articles.
ReplyIt looks like you spend a lot of effort and time on your blog.Keep Doing.
Digital Marketing Training in Bangalore
Digital Darketing Courses in Bangalore
Best Digital Marketing Courses in Bangalore
Digital Marketing Institute in Bangalore
Devops Training and Certification in Bangalore
Best Devops Training in Bangalore
Thanks for this kind of worthy information. this was really very helpful to me. keep continuing.
ReplyBest TOEFL Coaching Institute in Tambaram
TOEFL Course in Tambaram East
TOEFL Centres in Pallavaram
TOEFL Centres in Shollinganallur
TOEFL Training at Padur
TOEFL Classes in OMR
TOEFL Classes in Navalur
Thanks for given such informative and valuable blog. Visit for
ReplySEO Service in Delhi
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.
Replydevops Training in Nolambur
devops Training in Perambur
devops Training in Mogappair
devops Training in Thirumangalam
The information you have shared is more useful to us. Thanks for your blog.
ReplyList of Franchise Business in India
Franchise Opportunities in India with Low Investment
Best Franchise Business in India
Frenchies in India
Top Franchise in
India
Its my great pleasure to be here on your article!! for sure ill be back to read the next blog of yours.
ReplySelenium Training in Chennai
selenium testing training in chennai
iOS Training in Chennai
French Classes in Chennai
Big Data Training in Chennai
cloud computing certification courses in chennai
best cloud computing training institutes in chennai
Great. Having valuable information which help for youngster to understand clearly about C#.
Replyjava coaching centers in bangalore
This is best one article so far I have read online, I would like to appreciate you for making it very simple and easy
ReplyRegards,
Data Science Certification Training
Good Information and thanks for this great content. Keep Sharing
ReplyHadoop Training in Bangalore
Very good to read the post
Replyccna training in kk nagar
Amazing post thanks for the blog
ReplyBest cloud computing course in chennai
Good Post with really useful content, thanks for sharing with us.
ReplyC++ Training in Bangalore | IT Training in Bangalore
You are doing a great job. I would like to appreciate your work for good accuracy
ReplyMachine Learning in Chennai
ReplyYour Are doing a great job. I would like to appreciate your work for good accuracy
Blockchain Training in Chennai
Blockchain course in Chennai
Best Blockchain Training Institute in Chennai
Blockchain Certification in Chennai
Amazing post thanks for sharing
Replybest devops training institute in chennai
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.
ReplySOFTWARE TRAINING IN CHENNAI
POWERBI TRAINING IN CHENNAI
Post a Comment
Note: Only a member of this blog may post a comment.