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

241
Vistas
Search full string with different lengths in a char Array

I am working with an array with unknown length. I am looking for a string with a specific length that is given to me on the first two chars of the array. The array is filled with chars (hex values) and i am able to calculate the length of the first string, but i can't get the string nor get the next string length.

For example:

Buffer that reach to me from server:

var client = new net.Socket();
var res = Buffer.alloc(1);

client.on('data', function(data)){
     res = Buffer.from(data);
     console.log(res);
     var resArray = Array.from(res);
     resolveCommand(resArray);
});

<Buffer 03 53 65 60 05 68 65 6c 6c 6f>

I can calculate that the first string have 3 chars and the second got 5 chars, but for some reason i can't separate the strings ( the example above don't match reality, i could have 956 chars). I need to store and print the strings for further use.

below I leave the code that isn't working until now.

responseBar is an array of chars that's why I need to convert it to string.

calcNumb(hexToDecimal(lengthName) is calculating the length of the string and it's working. I double it because I need pair of chars for the translation.

const responseBar = ['0', '3', '5', '3', '6', '5', '6', '0', '0', '5', '6', '8', '6', '5', '6', 'c', '6', 'f']

 function hexToDecimal(datalength){                                                                      
     return parseInt(datalength, 16); 
 };

 function calcNumb(numberBytes){                                                       
    return Math.ceil((numberBytes * 6) /8);
 };
function resolveCommand(responseBar) {
  console.log(responseBar);
  var x = responseBar.toString();
  console.log('flag 1');
  for (var i = 0; i < responseBar.length - 1; i++) {
    console.log('flag 3');
    console.log("valor de i: " + i);
    var temp1 = responseBar[i];
    var temp2 = responseBar[i + 1];
    var lengthName = temp1 + temp2;
    var numbers = (calcNumb(hexToDecimal(lengthName)) * 2);
    console.log("NUMBER:  " + numbers);
    console.log('flag 3');
    console.log(lengthName);

  }
}

resolveCommand(responseBar)

about 4 years ago · Juan Pablo Isaza
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