Using QT with libmodbus, I read 2 registers from the Delta DVP PLC to the variable uint16_t deword_buf[2]; , in the end I get: deword_buf[0] = 0xCB71, deword_buf[1] = 0x04A2 (hex 04A2CB71 = 77777777 dec). I did not find the built-in function of modbus_get_float () in the description of libmodbus. Tell uint16_t deword_buf[2]; how to convert uint16_t deword_buf[2]; in long lSomeVar ?

  • deword_buf [1] << 16 + deword_buf [0] not? - Vladimir Martyanov Nov.

1 answer 1

Try this way.

 ( long )deword_buf[1] << 16 | deword_buf[0] 

Here is a demo program.

 #include <iostream> #include <iomanip> #include <cstdint> int main() { uint16_t deword_buf[] = { 0xCB71, 0x04A2 }; std::cout << std::hex << ( ( long )deword_buf[1] << 16 | deword_buf[0] ) << std::endl; return 0; } 

Its output to the console

 4a2cb71 
  • Thanks for the hint, long lSomeVar = ( long )deword_buf[1] << 16 | deword_buf[0] long lSomeVar = ( long )deword_buf[1] << 16 | deword_buf[0] , worked as it should - pandora
  • @pandora Not at all. - Vlad from Moscow
  • @pandora mark the answer with a decision - best of all thanks :) - Monah Tuk