“Base64 bis Base64url JavaScript” Code-Antworten

JavaScript Base64 Encode

var string = "Hello folks how are you doing today?";
var encodedString = btoa(string); // Base64 encode the String
var decodedString = atob(encodedString); // Base64 decode the String
Grepper

JS Base64 Encodes

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

Base64 bis 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, '');
}
Jolly Jay

Ähnliche Antworten wie “Base64 bis Base64url JavaScript”

Fragen ähnlich wie “Base64 bis Base64url JavaScript”

Weitere verwandte Antworten zu “Base64 bis Base64url JavaScript” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen