Reputation: 5080
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("-", "");
}
Upvotes: 29
Views: 18108
Reputation: 16505
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.
Upvotes: 13
Reputation: 74530
The ToString
method on BitConverter
is going to give you the byte array in a hexdecimal representation (base 16).
The ToBase64String
method on the Convert
class will give you a base 64-encoded string.
Both of them perform the function of converting a byte array into a string representation which can then be reversed (it is not a one-way transformation).
It is typically better to use the ToBase64String
method on Convert
if you have concerns about the length of the resulting string. Because base-64 can have a character in the base represent a larger number of values (4x more than base-16), it can represent a large number using a smaller number of characters.
If you are concerned about readability (for example, displaying the bytes in a file to a programmer, or a display where byte data is going to be displayed) then it makes more sense to use the ToString
method on BitConverter
, as most people are accustomed to viewing byte data in hexidecimal (base-16) format.
Upvotes: 11
Reputation: 56391
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()
.
Upvotes: 17