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

212
Views
Javascript Converting an incoming buffer as a string to a buffer

const stringArray = ['0x00','0x3c','0xbc]

to

const array = [0x00,0x3c,0bc]

var buf = new Buffer.from(array)

How should I go about using the buffers in the string above as buffers?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You appear to have an array of strings where the strings are byte values written as hexadecimal strings. So you need to:

  • Convert each hex string to a byte; that's easily done with parseInt(str, 16) (the 16 being hexadecimal). parseInt will allow the 0x prefix. Or you could use +str or Number(str) since the prefix is there to tell them what number base to use. (More about various ways to convert strings to numbers in my answer here.)
  • Create a buffer and fill it in with the bytes.

If the array isn't massive and you can happily create a temporary array, use map and Buffer.from:

const buffer = Buffer.from(theArray.map(str => +str)));

If you want to avoid any unnecessary intermediate arrays, I'm surprised not to see any variant of Buffer.from that allows mapping, so we have to do those things separately:

const buffer = Buffer.alloc(theArray.length);
for (let index = 0; index < theArray.length; ++index) {
    buffer[index] = +theArray[index];
}
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!