Provides full cross browser (and legacy browser) support for Base64 Encoding and Decoding true (not just merican) UTF-8 charset strings in Javascript. Will work with Base64 encoding and the UTF-8 charset on the .NET platform.
It's a complete wonder that we in 2020 still don't have a decent international charset support for a standard nearly 30 years old, used by the majority of the world.
var encodedString = base64.encode(decodedString)
OR
var encodedString = btoa(decodedString)
var decodedString = base64.decode(encodedString)
OR
var decodedString = atob(encodedString)
var encodedString = Convert.ToBase64String(
Encoding.UTF8.GetBytes(
HttpUtility.UrlEncode("Decoded string")))
var decodedString = HttpUtility.UrlDecode(
Encoding.UTF8.GetString(
Convert.FromBase64String("RW5jb2RlZCUyMHN0cmluZw==")))
public static class StringExtensions
{
public static string Base64Encode(this string str) =>
Convert.ToBase64String(Encoding.UTF8.GetBytes(HttpUtility.UrlEncode(str ?? string.Empty)));
public static string Base64Decode(this string str) =>
HttpUtility.UrlDecode(Encoding.UTF8.GetString(Convert.FromBase64String(str)));
}
var encodedString = "Decoded string".Base64Encode()
var decodedString = "RW5jb2RlZCUyMHN0cmluZw==".Base64Decode()