I'm trying to convert values of integers and unsigned integers to numbers and would like to know what format to use. I am a little confused and would like some clarification.
when converting int to a number do i use little endian format or LSB&MSB format?
the same with uint....do i convert uint using the little endian format or LSB&MSB?
could you also state the reason for the choice. any help is much appreciated. Thanks. Below are the two different functions to convert bytes to numbers. what are the main differences between them? in what case would you use one over the other?
export function convertFromLittleEndianByteToNumber(byteArray) {
if (!(byteArray && byteArray.length)) {
return;
}
let value = 0;
for (var i = byteArray.length - 1; i >= 0; i--) {
value = value * 256 + byteArray[i];
}
return value;
}
export function convertFromLSBandMSBToNumber(byteArray) {
if (!(byteArray && byteArray.length)) {
return;
}
const [LSM, MSB] = byteArray;
let converted = (LSM | (MSB << 8)) & 0xffff;
if (converted > MAX_POSITIVE_VALUE) {
converted = converted - MAX_16_BIT;
}
return converted;
}