base64 code example

Example 1: buffer from base64

const b64string = /* whatever */;
const buf = Buffer.from(b64string, 'base64');

Example 2: write a program to decrypt the following base64 encoded text

import base64
decoded_data = base64.b64decode("RW5jb2RlIHRoaXMgdGV4dA==")

print("decoded text is ")
print(decoded_data)

Example 3: JavaScriptSerializer() and convert to base64

class Program
{
    static void Main(string[] args)
    {
        var account = new ExternalAccount() { Name = "Someone" };
        string json = JsonConvert.SerializeObject(account);
        string base64EncodedExternalAccount = Convert.ToBase64String(Encoding.UTF8.GetBytes(json));
        byte[] byteArray = Convert.FromBase64String(base64EncodedExternalAccount);

        string jsonBack = Encoding.UTF8.GetString(byteArray);
        var accountBack = JsonConvert.DeserializeObject<ExternalAccount>(jsonBack);
        Console.WriteLine(accountBack.Name);
        Console.ReadLine();
    }
}

[Serializable]
public class ExternalAccount
{
    public string Name { get; set; }
}

Example 4: base64

function getBase64(file:any) {
    return new Promise((resolve, reject) => {
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = () => resolve(reader.result);
      reader.onerror = error => reject(error);
    });
  }

Example 5: Int64

decimal[] values= { Decimal.MinValue, -1034.23m, -12m, 0m, 147m,
                    199.55m, 9214.16m, Decimal.MaxValue };
long result;

foreach (decimal value in values)
{
   try {
      result = Convert.ToInt64(value);
      Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
                        value.GetType().Name, value,
                        result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("{0} is outside the range of the Int64 type.",
                        value);
   }   
}                                  
// The example displays the following output:
//    -79228162514264337593543950335 is outside the range of the Int64 type.
//    Converted the Decimal value '-1034.23' to the Int64 value -1034.
//    Converted the Decimal value '-12' to the Int64 value -12.
//    Converted the Decimal value '0' to the Int64 value 0.
//    Converted the Decimal value '147' to the Int64 value 147.
//    Converted the Decimal value '199.55' to the Int64 value 200.
//    Converted the Decimal value '9214.16' to the Int64 value 9214.
//    79228162514264337593543950335 is outside the range of the Int64 type.

Example 6: Int64

ulong ulNumber = 163245617943825;
try {
   long number1 = (long) ulNumber;
   Console.WriteLine(number1);
}
catch (OverflowException) {
   Console.WriteLine("{0} is out of range of an Int64.", ulNumber);
}

double dbl2 = 35901.997;
try {
   long number2 = (long) dbl2;
   Console.WriteLine(number2);
}   
catch (OverflowException) {
   Console.WriteLine("{0} is out of range of an Int64.", dbl2);
}
   
BigInteger bigNumber = (BigInteger) 1.63201978555e30;
try {
   long number3 = (long) bigNumber;
   Console.WriteLine(number3);
}
catch (OverflowException) {
   Console.WriteLine("{0} is out of range of an Int64.", bigNumber);
}    
// The example displays the following output:
//    163245617943825
//    35902
//    1,632,019,785,549,999,969,612,091,883,520 is out of range of an Int64.