Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BitConverter.ToString() vs Convert.ToBase64String()

Tags:

I had thought that Convert.ToBase64String() was the method to use to create a base64 string of a byte array, but I recently came across BitConverter.ToString(). What is the difference between the two?

And more specifically when should one be used over the other?


For example in this question about creating a MD5 digest, a comment by CraigS on an answer states "ToBase64String doesn't return what I want. However, BitConverter.ToString around the byte array does the trick."

BitConverter.ToString(     MD5.Create().ComputeHash(Encoding.Default.GetBytes(StringToEncode)) ).Replace("-", "") 

vs

Convert.ToBase64String(     MD5.Create().ComputeHash(Encoding.Default.GetBytes(StringToEncode)) ) 

Also, what should be used to encode images to base64?

public string ImageToBase64(int Img_ID) {     byte[] tempBytes = showImageById(Img_ID); // get image from DB     return Convert.ToBase64String(tempBytes); } 

vs

public string ImageToBase64(int Img_ID) {     byte[] tempBytes = showImageById(Img_ID); // get image from DB     return BitConverter.ToString(tempBytes).Replace("-", ""); } 
like image 681
Eddie Avatar asked Feb 25 '10 21:02

Eddie


People also ask

What is ToBase64String?

ToBase64String(Byte[]) Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with base-64 digits. ToBase64String(Byte[], Base64FormattingOptions) Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with base-64 digits.

What is FromBase64String in C#?

FromBase64String(String) method in C# converts the specified string, which encodes binary data as base-64 digits, to an equivalent 8-bit unsigned integer array.


2 Answers

BitConverter.ToString does not Base64 encode, it converts to hyphenated hexadecimal (Base 16 with dashes between each byte).

Ultimately, use the one that makes sense for your particular use. If you're sending bits across a text medium (e.g. http) use Base64, as you'll have to transmit less overall data.

If you're just wanting to display a text representation of some binary data to the user, you might use BitConverter.ToString().

like image 165
Randolpho Avatar answered Nov 21 '22 15:11

Randolpho


From MSDN for Convert.ToBase64String:

The base-64 digits in ascending order from zero are the uppercase characters "A" to "Z", the lowercase characters "a" to "z", the numerals "0" to "9", and the symbols "+" and "/". The valueless character, "=", is used for trailing padding.

The wikipedia article on Base64 is much more enlightening about how the algorithm actually works.

The BitConverter takes each byte's hex value as two digits and appends them one after another separated by a dash.

Both can be converted both ways.

For readability, the BitConverter beats the Base64 string any day, but the Base64 string is more compact.

like image 37
David Morton Avatar answered Nov 21 '22 14:11

David Morton