// Float is 32-bit (4 bytes) function hex2float(num) { var sign = (num & 0x80000000) ? -1 : 1; // 1 bit. To check the sign bit on most left var exponent = ((num >> 23) & 0xff) - 127; // 8 bits. 8 right bits. -127 to 128 var mantissa = 1 + ((num & 0x7fffff) / 0x7fffff); // 23 bits return parseFloat((sign * mantissa * Math.pow(2, exponent)).toFixed(3)); } // Double is 64-bit (8 bytes) function hex2double(left, right) { var sign = (left & 0x80000000) ? -1 : 1; // 1 bit. To check the sign bit on most left var e = (left >> 52 - 32 & 0x7ff) - 1023 return (left & 0xfffff | 0x100000) * 1.0 / Math.pow(2,52-32) * Math.pow(2, e) + right * 1.0 / Math.pow(2, 52) * Math.pow(2, e) } // For UPLINK packets function Decode(fPort, bytes, variables) { // bytes[0] is payload version. Can ignore it // To check length: bytes.length > x // If > 12 bytes, must set AT+DWELLT=0 // To start from bytes[1] // Float (4 bytes) var float1=hex2float(bytes[1] << 24 | bytes[2] << 16 | bytes[3] << 8 | bytes[4]); // Double (8 bytes) var left2=bytes[5] << 24 | bytes[6] << 16 | bytes[7] << 8 | bytes[8]; var right2=bytes[9] << 24 | bytes[10] << 16 | bytes[11] << 8 | bytes[12]; var double2=hex2double(left2, right2); // Simple float number (2 bytes), xx.x var simple_float3=(bytes[13] << 8 | bytes[14]) / 10; // Unsigned integer (2 bytes), xx var unsigned_int4=bytes[15] << 8 | bytes[16]; // Unsigned long (4 bytes), xx var unsigned_long5=bytes[17] << 24 | bytes[18] << 16 | bytes[19] << 8 | bytes[20]; return {"float1": float1, "double2": double2, "simple_float3": simple_float3, "unsigned_int4":unsigned_int4, "unsigned_long5": unsigned_long5}; }