Convert .net String object into base64 encoded string

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));

Java Base64 encoded string vs. .NET Base64 encoded string

Problem: I have a .NET HTTP Handler taking an HTTP POST of XML which is originating, I believe, from a Java system. One element contains a base64 string encoded document (current test file is a PDF).

Android convert base64 encoded string into image view

I want to convert base64 encoded string into bitmap so i can put it in image view, but getting error like D/skia(7490): — decoder->decode returned false and bitmap returns null value My code is:

java string base64 encoded

I need a way to know if a string is Base64 encoded… any idea ? thanks

How to convert uint8 Array to base64 Encoded String?

I got a webSocket comunication, I recieve base64 encoded string, convert it to uint8 and work on it, but now I need to send back, I got the uint8 array, and need to convert it to base64 string, so I c

Convert Base64 String with Gwt

How to convert a base64 String to byte array with Gwt client side code? This link shows a encode decode, for base64 but not to byte[] array

Is a Base64 encoded string completely alphanumeric?

Is a Base64 encoded string completely alphanumeric except for the = at the end?

ArrayBuffer to base64 encoded string

I need an efficient (read native) way to convert an ArrayBuffer to a base64 string which needs to be used on a multipart post.

Convert from Base64 String to PNG File

I am trying to convert a base64 encoded string to an image object using the method below. function gotFileWriter(writer) { console.log(‘Starting gotFileWriter’); writer.onwrite = function (evt) { cons

Append newline to a Base64 encoded string.

byte[] serObj = getBytesFromFile(file); final byte[] CLRF = { ‘/r’, ‘/n’ }; Base64 encoded = new Base64 (72,CLRF); System.out.println(encoded.encodeBase64String(serObj)); I am having problems formatt

Generating PublicKey from Base64 encoded String

I need to use PublicKey to encrypt session key but I have public key as Base64 encoded string so I tried to convert it as : KeyFactory kf = KeyFactory.getInstance(RSA); // or EC or // whatever byt


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.

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

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.

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));
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