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

356
Visualizações
Encoding Text to Base64 and decoding back to JSON

I am trying to encode a text to Base64 and using NodeJS, and then while getting the data I am decoding it back from Base64. Now I need to change the data into JSON so I could fill the relevant fields but its not converting to JSON.

Here is my code:

fs.readFile('./setting.txt', 'utf8', function(err, data) {
    if (err) throw err;
    var encodedData = base64.encode(data);

    var decoded = base64.decode(encodedData).replace(/\n/g, '').replace(/\s/g, " ");
    
   return res.status(200).send(decoded);

    });

In setting.txt I have the following text:

LENGTH=1076
CRC16=28653
OFFSET=37
MEASUREMENT_SAMPLING_RATE=4
MEASUREMENT_RANGE=1
MEASUREMENT_TRACE_LENGTH=16384
MEASUREMENT_PRETRIGGER_LENGTH=0
MEASUREMENT_UNIT=2
MEASUREMENT_OFFSET_REMOVER=1

This decodes the result properly but when I use JSON.parse(JSON.stringify(decoded ) its not converting to JSON.

Can someone help me with it.

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

0

What does base-64 encoding/decoding have to do with mapping a list of tuples (key/value pairs) like this:

LENGTH=1076
CRC16=28653
OFFSET=37
MEASUREMENT_SAMPLING_RATE=4
MEASUREMENT_RANGE=1
MEASUREMENT_TRACE_LENGTH=16384
MEASUREMENT_PRETRIGGER_LENGTH=0
MEASUREMENT_UNIT=2
MEASUREMENT_OFFSET_REMOVER=1

into JSON?

If you want to "turn it (the above) into JSON", you need to:

  • Decide on what its JSON representation should be, then
  • Parse it into its component bits, convert that into an appropriate data struct, and then
  • use JSON.stringify() to convert it to JSON.

For instance:

function jsonify( document ) {
  const tuples = document
                 .split( /\n|\r\n?/ )
                 .map( x => x.split( '=', 2) )
                 .map( ([k,v]) => {
                   const n = Number(n);
                   return [ k , n === NaN ? v : n ];
                 });
  const obj = Object.fromEntries(tuples);

  const json = JSON.stringify(obj);
  return json;
}
over 4 years ago · Santiago Trujillo Relatório

0

Try below snippet

let base64Json=  new Buffer(JSON.stringify({}),"base64").toString('base64');
let json = new Buffer(base64Json, 'ascii').toString('ascii');
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