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

178
Visualizações
Read and echo image file Node.js

I have a small png file called small.png. I expect the following Node.js code to be equivalent to a cat of the file. However, I am clearly reading something incorrectly, as the result is different, and redirecting output into a new file results in something that is not even recognizable as a PNG.

node -e 'console.log(require("fs").readFileSync("static/small.png", "utf8").toString())'

Or, expanded:

const fs = require("fs");
console.log(fs.readFileSync("static/small.png").toString());

I suspected it could be an encoding issue, but "utf16le" seemed to look less like the original.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

A binary file can't be reproduced as a utf8 string, unless maybe if it happens to be a utf8 string in the first place.

fs.createReadStream defaults to no encoding and can be piped to the stdout of the node process.

const stream = require("fs").createReadStream("small.png")
stream.pipe(process.stdout)
% cat small.png | sha256sum
065453ee61387cec8383a2337c488a3f65e709f52637efd5f97c1efe6c333f2b  -

% node -e 'st = require("fs").createReadStream("small.png"); st.pipe(process.stdout)' | sha256sum
065453ee61387cec8383a2337c488a3f65e709f52637efd5f97c1efe6c333f2b  -
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