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)