Reading float from uplink buffer with readFloatBE()

I’d like to use the readFloatBE() function in my JavaScript codec. However, I get the following error:

"Exception generated by quickjs: ‘ieee754’ is not defined at readFloatBE (buffer:1385) at decodeUplink (eval_script:15) at (eval_script:20) "

The codec works fine when testing locally on NodeJS. Is this function not supported by quickjs? From the documentation I got the impression that the whole buffer class should be available.

Here is the codec (I have commented out parts for testing purposes):

function decodeUplink(input){
/*
  	sn : bytes_buf.readUIntBE(0,4),
      format : bytes_buf.readUIntBE(4,1),
      byte_count : bytes_buf.readUIntBE(5,1),
      voltage : +(bytes_buf.readFloatBE(6,4).toFixed(3)),
      amps : +(bytes_buf.readFloatBE(10,4).toFixed(3)),
      crc : bytes_buf.readUIntBE(18,2),
*/
        
const bytes_buf = Buffer.from(input.bytes)
  return{
    data:{
      Eapp : +(bytes_buf.readFloatBE(14).toFixed(3)),
    }
  }
}

Not a good solution but I had to do a quick & dirty on TTStack.
Requires hex string might get you going until definitive answer

function hexToIeee754(str) {
  var float = 0, sign, order, mantiss,exp,
      int = 0, multi = 1;
  if (/^0x/.exec(str)) {
    int = parseInt(str,16);
  }else{
    for (var i = str.length -1; i >=0; i -= 1) {
      if (str.charCodeAt(i)>255) {
        console.log('Wrong string parametr'); 
        return false;
      }
      int += str.charCodeAt(i) * multi;
      multi *= 256;
    }
  }
  sign = (int>>>31)?-1:1;
  exp = (int >>> 23 & 0xff) - 127;
  mantissa = ((int & 0x7fffff) + 0x800000).toString(2);
  for (i=0; i<mantissa.length; i+=1){
    float += parseInt(mantissa[i])? Math.pow(2,exp):0;
    exp--;
  }
  return  Number((float*sign).toFixed(2));
}

function padWithZero(num, targetLength) {
  return String(num).padStart(targetLength, '0');
}

function hexStr(hex){
  return padWithZero(hex.toString(16),2)
  
}
1 Like

Thanks for your feedback. I have a similarly hacky solution running at the moment which is working fine. I’m simply trying to clean up the decoders and the builtin function would really help with that.