Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

base64 to string and string to base64 javascript decode

// base64 to string
let base64ToString = Buffer.from(obj, "base64").toString();
base64ToString = JSON.parse(base64ToString);

//or
let str = 'bmltZXNoZGV1amEuY29t';
let buff = new Buffer(str, 'base64');
let base64ToStringNew = buff.toString('ascii');

// string to base64
let data = 'nimeshdeuja.com';
let buff = new Buffer(data);
let stringToBase64 = buff.toString('base64');
Comment

javascript encode base64

const encoded = window.btoa('Alireza Dezfoolian'); // encode a string
const decoded = window.atob(encoded); // decode the string
Comment

js base64 encoding

str = "The quick brown fox jumps over the lazy dog";
b64 = btoa(unescape(encodeURIComponent(str)));
str = decodeURIComponent(escape(window.atob(b64)));
Comment

base64 encode in javascript

const base64Encode = (string) => {
  const newText = btoa(string);
  return newText;
};
Comment

js base64 encode

function toBinary(string) {
  const codeUnits = new Uint16Array(string.length);
  for (let i = 0; i < codeUnits.length; i++) {
    codeUnits[i] = string.charCodeAt(i);
  }
  return String.fromCharCode(...new Uint8Array(codeUnits.buffer));
}

function fromBinary(binary) {
  const bytes = new Uint8Array(binary.length);
  for (let i = 0; i < bytes.length; i++) {
    bytes[i] = binary.charCodeAt(i);
  }
  return String.fromCharCode(...new Uint16Array(bytes.buffer));
}

const myString = "☸☹☺☻☼☾☿"
// console.log(btoa(myString)) // Error InvalidCharacterError: The string to be encoded contains characters outside of the Latin1 range.
const converted = toBinary(myString)
const encoded = btoa(converted)
console.log(encoded)

const decoded = atob(encoded)
const original = fromBinary(decoded)
console.log(original);
Comment

base64 in js

const Base64 = {
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
    encode: function(input: string) {
        let output = "";
        let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        let i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },
    _utf8_encode: function(input: string) {
        input= input.replace(/
/g, "
");
        let utftext = "";

        for (let n = 0; n < input.length; n++) {

            let c = input.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

}


const encode = ( cipher : string) => {

    const encodedString: string = Base64.encode(cipher)

    return encodedString;
}
Comment

javascript base2 to base64

const encode = bitstr => {
  const bytes = [];
  // convert bit string to BigInt
  let value = BigInt('0b' + bitstr);
  // chop it up into bytes
  while (value > 0n) {
    bytes.unshift(Number(value & 0xffn));
    value >>= 8n;
  }
  // convert to binary string and encode as base64
  return btoa(String.fromCharCode.apply(null, bytes));
};

const decode = b64 => {
  // decode base64 to binary string
  const bstr = atob(b64);
  // convert binary string to bit string
  return new Array(bstr.length).fill(0).map(
    (_,i) => bstr.charCodeAt(i).toString(2).padStart(8, i ? '0' : '')
  ).join('');
};

const bitstr = '100110110101000110100011011001100010110100011011001100100110100011000001100000110000011000001100001001010100111110000011001111100101010011111010011100010110001001001001100000110100111010010100111110000111001000100000110001001000101100111110011001001001101011010001011001001101001010000011000100100100110000011010011';
const encoded = encode(bitstr);
const decoded = decode(encoded);

console.log(bitstr);
console.log(encoded);
console.log(decoded);
 Run code snippetHide results
Comment

base64 to base64url javascript

/**
 * Function that converts a base64 string into a base64url string
 * @param {string} input - The string to convert
 */
function base64ToBase64url(input) {
  // Replace non-url compatible chars with base64url standard chars and remove leading =
  return input
    .replace(/+/g, '_')
    .replace(///g, '-')
    .replace(/=+$/g, '');
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: create and save xml file in javascript 
Javascript :: Find the count of a letter in a string 
Javascript :: return value 
Javascript :: send json in javascript 
Javascript :: call two functions onpress react native 
Javascript :: For-each over an array in JavaScript 
Javascript :: The ".charAt()" JavaScript string method 
Javascript :: show password eye icon angular 9 
Javascript :: sample promise.all javascript 
Javascript :: material ui phone number input 
Javascript :: jquery modal popup 
Javascript :: super keyword in javascript 
Javascript :: side effect, useEffect 
Javascript :: JS how to access a class propert 
Javascript :: binarysearch 
Javascript :: fetch in vue 3 
Javascript :: slide js 
Javascript :: for pug 
Javascript :: Set Default Parameter Value 
Javascript :: js filter array 
Javascript :: javascript regex zero or more occurrence 
Javascript :: javascript continue with while Loop 
Javascript :: could not find react-redux context value; please ensure the component is wrapped in a <Provider 
Javascript :: how to check if a variable is true or false in javascript 
Javascript :: javascript static 
Javascript :: how to make and add to an array in javascript 
Javascript :: import module in ES6 
Javascript :: array of objects in js 
Javascript :: namespace javascript 
Javascript :: react state lifting 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =