I want to read Data via node red modbus node from a data source. The range is -20000 to 20000, but the node cannot handle negative numbers, so I had to convert them to binary numbers (DWORD), split them in the lower and higher word and convert these words back to integers.
var low
function dec2bin(dec){
return (dec >>> 0).toString(2);
}
var a = msg.payload
if (a >= 0){
a = dec2bin(a);
a = parseInt(a,2);
} else {
a = dec2bin(a);
a = a.substr(16);
a = parseInt(a,2);
}
low = { payload: a };
return low;
For visualisation I want to use the dashboard nodes, but therefor I need to join the 2 binary strings together and convert them to an int.
Problem:
node red converts them as a qword, so the binary number 1111 1111 1111 1111 1111 1100 0001 1000 is seen as 4.294.966.296, not as -1000. But if i fill the next rest with 1 lime so: 1111 1111 1111 1111 1111 1111 1111 1111 1111 1100 0001 1000 puts out 18446744073709552000
Thanks
Convert String to int using Integer. String number = "1234"; int convertedNumber = Integer. parseInt(number); System. out. println("Convert String to int: " + convertedNumber); String negativeNumber = "-1234"; int convertedNegativeNumber = Integer.
Using two's complement for negative numbersAdd a 0 to the front of the number, to indicate that it is positive. Invert or find the complement of each bit in the number. Add 1 to this number.
The simplest is to simply use the leftmost digit of the number as a special value to represent the sign of the number: 0 = positive, 1 = negative. For example, a value of positive 12 (decimal) would be written as 01100 in binary, but negative 12 (decimal) would be written as 11100.
The decimal number is equal to the sum of binary digits (dn) times their power of 2 (2n): decimal = d0×20 + d1×21 + d2×22 + ...
parseInt
works on variable-length strings as input, which means that it cannot recognize the most significant bit as a sign bit. In other words: It parses binary strings like decimal strings, using a -
as sign; so you have to use a different method than parseInt
.
What hinders you from taking that 16-bit value and just pass it as unsigned? You'd just have to check whether the value coming in to your javascript is bigger than 32767, then do the two-complement conversion manually:
Your incoming numbers range would then be 0..20000 for positive "originals" and 45536..65535 for the original range -20000..-1 (if I'm thinking correct right now, I didn't verify that). This would mean that conversion is easy:
if( number > 32767 ) {
number = -65536 + number;
}
Or, for any binary number type (that is smaller than the number size of the language you're working in):
if( number > SIGNED_MAX_INT ) {
number = -(UNSIGNED_MAX_INT+1) + number;
}
(Those constants don't exist as written, they're more like pseudo code here)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With