pws-formula
Version:
This is a formula execute the string or buffer format formula libray to get values from string or buffer.
72 lines (66 loc) • 1.9 kB
JavaScript
/**
* Project: c:\work\PWS_Projects\03_PWS_Lib\pws-formula
* Created Date: Wednesday, August 22nd 2018, 12:01:16 pm
* Author: Thomas.li
* -----
* Last Modified:
* Modified By:
* -----
* Copyright (c) 2018 pareact
* ------------------------------------
* Javascript will save your soul!
*/
// let buf = Buffer.from(
// '51032400000000000E4A440003073200000000000E4A44000000000003073200030732001418D77D62',
// 'hex'
// );
let buf = Buffer.from(
'65034008E70F43021A1383006DFC80FFF5007A08D20F2A01ED13830063FC84FFFE006F08B60F5002091383006AFC67FFFF007308CE0F3D020413',
'hex'
);
/**
* get the float value from 4 byte buffer
* @param {number} start
*/
function hp(start) {
if (start < 0 || start > buf.length - 4) {
throw new RangeError('Out of the range');
}
let result = [...Array(4).keys()].map(i =>
buf[start + i].toString(2).padStart(8, '0')
);
result = result.join('');
let reg = /^[0-1]*$/;
if (!reg.test(result)) throw new TypeError('The value is invalid');
let mark = result[0] == '1' ? -1 : 1;
let eMark = parseInt(result.slice(1, 9), 2);
let mMark = parseInt(result.slice(9), 2);
let r1 = 2 ** (eMark - 127);
let r2 = mark * r1 * mMark * 2 ** -23;
return Math.round((r1 + r2) * 100) / 100;
}
/**
* get the Hex value of DEC
* @param {number} start
* @param {number} [length=2]
* @returns
*/
function hx(start, length = 2) {
if (length > 4 || length < 0) {
throw new RangeError('Out of Index Range');
}
return buf.readIntBE(start, length);
}
// console.log(hp(3));
// console.log(hp(7));
// console.log(hp(11));
// console.log(hp(15));
// console.log(hp(19));
// console.log(hp(23));
// console.log(hp(27));
// console.log(hp(31));
// console.log(hp(35));
// console.log(hp(39));
// console.log(hp(43));
// console.log(hp(47));
console.log(hx(11));