Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. 2's complement convertion
QtWS25 Last Chance

2's complement convertion

Scheduled Pinned Locked Moved Solved General and Desktop
complementtwoconvertbitstring
2 Posts 2 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GhostWolf
    wrote on last edited by
    #1

    Hello,

    I have build a programm that gets some data from a modbus device.
    The modbus device sends its data with the 2's complement way.

    How can i convert the incoming value to a normal integer?

    This is what is given with the device:

    Extended addressing
    The panels use extended (32-bit) addressing format.
    http://www.simplymodbus.ca/faq.htm#Ext
    Value = (65536 * MSW) + LSW
    How to interpret the values
    The two's complement of the value is used.
    http://en.wikipedia.org/wiki/Two%27s_complement
    32 bit example: 1 is represented as 0b00000000_00000000_00000000_00000001
    32 bit example: -1 is represented as 0b11111111_11111111_11111111_11111111
    

    This is the code i have now:

    auto replyIR = qobject_cast<QModbusReply *>(sender());
    const QModbusDataUnit unit = replyIR->result();
    uint32_t test2 = (unit.value(1)<<16) | unit.value(0);
    ui->r1->setText(QString::number(test2);
    

    (this works the same as (65536 * unit.value(1)) + unit.value(0); )

    This works fine for positive numbers. The problem is the negative numbers.
    How can i read all the numbers?

    JKSHJ 1 Reply Last reply
    0
    • G GhostWolf

      Hello,

      I have build a programm that gets some data from a modbus device.
      The modbus device sends its data with the 2's complement way.

      How can i convert the incoming value to a normal integer?

      This is what is given with the device:

      Extended addressing
      The panels use extended (32-bit) addressing format.
      http://www.simplymodbus.ca/faq.htm#Ext
      Value = (65536 * MSW) + LSW
      How to interpret the values
      The two's complement of the value is used.
      http://en.wikipedia.org/wiki/Two%27s_complement
      32 bit example: 1 is represented as 0b00000000_00000000_00000000_00000001
      32 bit example: -1 is represented as 0b11111111_11111111_11111111_11111111
      

      This is the code i have now:

      auto replyIR = qobject_cast<QModbusReply *>(sender());
      const QModbusDataUnit unit = replyIR->result();
      uint32_t test2 = (unit.value(1)<<16) | unit.value(0);
      ui->r1->setText(QString::number(test2);
      

      (this works the same as (65536 * unit.value(1)) + unit.value(0); )

      This works fine for positive numbers. The problem is the negative numbers.
      How can i read all the numbers?

      JKSHJ Online
      JKSHJ Online
      JKSH
      Moderators
      wrote on last edited by
      #2

      @GhostWolf said in 2's complement convertion:

      uint32_t test2 = (unit.value(1)<<16) | unit.value(0);

      Just store the result as a signed integer (int32_t) instead of unsigned (uint32_t).

      You don't need to change the bit pattern (0b11111111_11111111_11111111_11111111).

      • Interpreting this pattern as Unsigned gives you ‭4294967295
      • Interpreting this pattern as Signed gives you -1.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved