35

I have a question, which Unicode encoding to use while encoding .NET string into base64? I know strings are UTF-16 encoded on Windows, so is my way of encoding is the right one?

public static String ToBase64String(this String source) {
        return Convert.ToBase64String(Encoding.Unicode.GetBytes(source));
    }

4 Answers 4

28

What you've provided is perfectly functional. It will produce a base64-encoded string of the bytes of your source string encoded in UTF-16.

If you're asking if UTF-16 can represent any character in your string, then yes. The only difference between UTF-16 and UTF-32 is that UTF-16 is a variable-length encoding; it uses two-bytes to represent characters within a subset, and four-bytes for all other characters.

There are no unicode characters that cannot be represented by UTF-16.

1
  • 2
    "There are no unicode characters that cannot be represented by UTF-16." Or indeed by any of the other transformations (UTFs). Dec 30, 2014 at 11:39
3

Be aware that you don't have to use UTF-16 just because that's what .NET strings use. When you create that byte array, you're free to choose any encoding that will handle all the characters in your string. For example, UTF-8 would be more efficient if the text is in a Latin-based language, but it can still handle every known character.

The most important concern is that whatever software decodes the base64 string, needs to know which encoding to apply to the byte array to re-create the original string.

3

Here is the Solution, I have converted a Random string conversion like you can give any size up to 10 that Base64 will output.

//This function will return a random string from the given numeric characters
public string RandomString(int size)
{
const string legalCharacters = "1234567890";
Random random = new Random();
StringBuilder builder = new StringBuilder();
char ch = '\0';

for (int i = 0; i <= size - 1; i++) {
    ch = legalCharacters(random.Next(0, legalCharacters.Length));
    builder.Append(ch);
}
return builder.ToString();
}
public const string BASE64 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
public string DecToBase64(long lVal)
{
string sVal = null;
sVal = "";
while (lVal >= 64) {
    sVal = sVal + DecToBase64(lVal / 64);
    lVal = lVal - 64 * (lVal / 64);
}
sVal = sVal + Strings.Mid(BASE64, Convert.ToInt32(lVal) + 1, 1);
return sVal;
}

//here is how we can have result in variable:
string Base64 = "";
Base64 = DecToBase64(RandomString(10)); //this will produce a combination up-to length of 10
2

MSDN confirms that UnicodeEncoding class represents a UTF-16 encoding of Unicode characters.

1
  • If my string will have not only English letters and decimal digits in it, will it still work properly?
    – chester89
    Apr 13, 2010 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.