Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda