Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert negative binary number to int?

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

like image 308
c0nr3f Avatar asked May 13 '20 12:05

c0nr3f


People also ask

How do you convert a negative number to int?

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.

How do you convert negative binary to positive?

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.

How do you translate negative numbers into binary?

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.

How do you convert binary to integer?

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 + ...


1 Answers

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)

like image 58
orithena Avatar answered Oct 29 '22 00:10

orithena