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

660
Views
JavaScript Node.Js Buffers to Binary

I am sending and receiving packets with the help of Node.JS Buffers but I am not sure how to convert these buffers into a binary representation. I tried this but it did not work

let buff = Buffer.alloc(10);
buff[0] = 10;
buff[1] = 16;
buff[2] = 20;
buff[3] = 32;
buff[4] = 9;

console.log(buff);
console.log(buff.toString('binary'));
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Acording to the documentation 'binary' is an alias for 'latin1'

If you want a binary representation the easiest way to get a string representation of the buffer with only 1 and 0 is to convert the buffer to hexadecimal his representation, then parse this representation into a BigInt, get the base 2 of the BigInt, and finally pad the string with non significant zeros

function buf2bin (buffer) {
  return BigInt('0x' + buffer.toString('hex')).toString(2).padStart(buffer.length * 8, '0')
}
over 4 years ago · Santiago Trujillo Report

0

If what you're asking is you want a string representation (expressed in binary) of each separate byte in the Buffer all combined into one string with space separators for each byte, then you can do that like this:

function convertBufferToBinaryString(buf, len) {
    let result = [];
    for (let i = 0; i < len; i++) {
        result.unshift(buf[i].toString(2).padStart(8, "0"));
    }
    return result.join(" ");
}

// sample usage

let buff = Buffer.alloc(10);
buff[0] = 10;
buff[1] = 16;
buff[2] = 20;
buff[3] = 32;
buff[4] = 9;

let str = convertBufferToBinaryString(buff, 5);
console.log(str);

Since this is a custom output format, there is no built-in way to do this. You can convert any single number to a binary string representation using .toString(2), but to combine the binary string representation of each separate byte in the buffer, you would have to build that combined result yourself (as shown above).

Note: This implementation assumes you want the [0] element of the buffer at the right end of the result string. That's why it uses .unshift() when building the result array. If you want the [0] element at the left end of the string, then use .push() instead.

over 4 years ago · Santiago Trujillo Report

0

Simple reduce to string with padStart combination.

const buff = Buffer.alloc(10);
buff[0] = 10;
buff[1] = 16;
buff[2] = 20;
buff[3] = 32;
buff[4] = 9;

const paddedBinString = buff.reduce(
    (acc, byte) => (acc += byte.toString(2).padStart(8, "0")),
    ""
);

console.log(paddedBinString);
// 00001010 00010000 00010100 00100000 00001001 00000000 00000000 00000000 00000000 00000000
over 4 years ago · Santiago Trujillo 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!