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

500
Visualizações
Saving hex data to file in NodeJS

Say I've got a string

"020000009B020000C0060000"

and I want to turn it into a string such that if it was saved as a file and opened it in a hex editor it would display the same hex as I put in. How do I do this? I've never had to use buffers or anything, but in trying to do this I've seemingly had to, and I don't really know what I'm doing.

(or whether I should be using buffers at all- I've been searching for hours and all I can find are answers that use buffers so I've just taken their examples and yet nothing is working)

I turned the hex string into a buffer

function hexToBuffer(hex) {
  let typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
    return parseInt(h, 16)
  }))
  return typedArray
}

and this seemed to work because logging hexToBuffer("020000009B020000C0060000").buffer what it returns gave me this:

ArrayBuffer {
  [Uint8Contents]: <02 00 00 00 9b 02 00 00 c0 06 00 00>,
  byteLength: 12
}

which has the same hex as I put in, so it seems to be working fine, then to make the array buffer into a string I did this.

let dataView = new DataView(buffer);
let decoder = new TextDecoder('utf-8');
let string =  decoder.decode(dataView)

just to test that it worked, I saved it to a file.

fs.writeFileSync(__dirname+'/test.txt', string)

Opening test.txt in a hex editor shows different data: 02000000EFBFBD020000EFBFBD060000

If I instead do

fs.writeFileSync(__dirname+'/test.txt', hexToBuffer("020000009B020000C0060000"))

then I get the correct data- but then if I read the file with fs and then add to it it's once again not the same values.

let test = fs.readFileSync(__dirname+'/test.txt', 'utf8)
let example2 = test+'example'
fs.writeFileSync(__dirname+'/test.txt', example2)

now test.txt begins with 02000000EFBFBD020000EFBFBD060000 instead of 020000009B020000C0060000. What do I do?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

First of all, you can use the Buffer.from(string[, encoding]) method to create a Buffer from a string whilst also specifying the encoding - in your case it will be "hex":

const b1 = Buffer.from("020000009B020000C0060000", "hex")

Now save it to a file:

fs.writeFileSync(path.resolve('./test'), b1)

Then we can check that the file contains the same hex values as in the string by using xxd on the command line:

$ xxd test
0200 0000 9b02 0000 c006 0000

Looking good!

Now we can read the file back into a buffer as well, making sure to tell it the encoding of the file is "hex" again:

const b2 = fs.readFileSync(path.resolve("./test"), "hex")

Finally, turn it back into a string again with the Buffer.toString() method:

console.log(b2.toString()) // => "020000009b020000c0060000"
over 4 years ago · Santiago Trujillo 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