最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I convert negative binary number to int? - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited May 13, 2020 at 14:12 c0nr3f asked May 13, 2020 at 12:51 c0nr3fc0nr3f 871 silver badge8 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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 ing in to your javascript is bigger than 32767, then do the two-plement conversion manually:

Your ining 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)

First, it is REALLY bad practice to create new msg objects in function nodes, you should be just updating the msg.payload value and passing on the original object.

Next the easiest way to do this will be to work with a Buffer

e.g.

var b = Buffer(4)
b.writeUInt32BE(msg.playload)
msg.payload = b.readInt32BE()

return msg;

A join node in manual mode can bine the 2 smaller buffers into one of 16bits in length.

Well, JavaScript is lame at this point because it does not have strong types to do this. Probably not the best, but one of the ways to do this is creating custom Int class.

class Int {
  constructor(length, binaryStr) {
    const cleanBinary = "0".repeat(length - binaryStr.length) + binaryStr
    if (cleanBinary.startsWith("1")) {
      const invertedBinary = cleanBinary.split("")
        .map(char => char === "1" ? "0": "1")
        .join("")
      this.decValue = -parseInt(invertedBinary, 2) - 1
    } else {
      this.decValue = parseInt(cleanBinary, 2)
    }
  }
}

Something like that

new Int(8, "11111100")
> Int {decValue: -4}
strBin = "11111111111111111111110000011000";
new Int(strBin.length, strBin)
> Int {decValue: -1000}

This may be developed into something more advanced and can be used in browser as well.

发布评论

评论列表(0)

  1. 暂无评论