Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

331
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!