Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
Javascript - looking for most efficient way to convert a JSON object into a null (<0x00>)-terminated byte array

Per https://www.jsonrpc.org/specification I need a JSON object returned as a byte array, contatenated with <0x00>.

Consider this object:

{"jsonrpc":"2.0","method":"StatusGet","id":"1","params":0}

Returned as <0x00>-terminated byte array:

[123,34,106,115,111,110,114,112,99,34,58,34,50,46,48,34,44,34,109,101,116,104,111,100,34,58,34,83,116,97,116,117,115,71,101,116,34,44,34,105,100,34,58,34,49,34,44,34,112,97,114,97,109,115,34,58,48,125,0]

From examples found elsewhere, here's what I currently have that works (in a Nodered function node, which is fed into a TCP request node):

function fnStringMessage(str){
    
    let bytes = Buffer.from(str)
    bytes = []
    for (let i=0; i<str.length; i++){
        bytes.push(str.charCodeAt(i))
    }
    bytes.push(0)

    return (bytes)
}

let ss = JSON.stringify(msg.payload)

msg.payload = Buffer.from(fnStringMessage(ss))

return msg;

However I'm a noob and using a for loop looks a bit turtle to me. Looking for the hare, and any other suggestions to simplify this further. Thanks

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can append a zero byte to the string, then convert that into a Buffer.

function fnStringMessage(str){
  return Buffer.from(str + "\0");
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda