Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

332
Vistas
How to convert a bit string to base64 in node.js?

I have a bit string that I want to convert to base64 but it doesn't look like there's a native function to do this and I couldn't find a node module either. ):

Input: 100110110101000110100011011001100010110100011011001100100110100011000001100000110000011000001100001001010100111110000011001111100101010011111010011100010110001001001001100000110100111010010100111110000111001000100000110001001000101100111110011001001001101011010001011001001101001010000011000100100100110000011010011

Output: base64 representation of that equivalent binary value

Maybe a better question is how to convert a bit string into a buffer? Not sure

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As you guessed, the main thing is converting the string into something easier to convert to base64 and then converting that to base64.

In the code below, we do these conversion sequences:

  • bit string -> BigInt -> array of byte-sized ints -> binary string -> base64
  • base64 -> binary string -> array of byte-sized bit strings -> bit string

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda