Extract Number Form A Hexadecimal Dataframe Parseint()parsefloat()
Im trying to parse the data coming from some sensors, The data I recive is a hexadecimal number f.e.: '450934644ad7a38441' I want to extract the last 8 characters, that are the val
Solution 1:
I think this is what you need. String received by function yourStringToFloat
is your data
variable received by main function in your example. Of course you can fix precision according to your needs.
functionflipHexString(hexValue, hexDigits) {
var h = hexValue.substr(0, 2);
for (var i = 0; i < hexDigits; ++i) {
h += hexValue.substr(2 + (hexDigits - 1 - i) * 2, 2);
}
return h;
}
functionhexToFloat(hex) {
var s = hex >> 31 ? -1 : 1;
var e = (hex >> 23) & 0xFF;
return s * (hex & 0x7fffff | 0x800000) * 1.0 / Math.pow(2, 23) * Math.pow(2, (e - 127))
}
functionyourStringToFloat(str){
returnhexToFloat(flipHexString("0x"+str.substr(10), 8))
}
console.log(yourStringToFloat('450934644ad7a38441'));
Post a Comment for "Extract Number Form A Hexadecimal Dataframe Parseint()parsefloat()"