Results 1 to 4 of 4

Thread: quint16 to QByteArray Conversion

  1. #1
    Join Date
    Jan 2013
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default quint16 to QByteArray Conversion

    Hello All,

    I begin to work with Qt so sorry if the answer is easy for you.
    I am working on a software which communicates with a device.
    I use the QextSerialPort librairie and it is working fine.

    In fact, it is working fine when I send to my device a constant frame.
    Qt Code:
    1. /* Erasing Command */
    2. static const char eraseCmd[] = {0x11, 0x02, 0x00, 0x00, 0x00, 0x02 };
    3. QByteArray Command = QByteArray::fromRawData(eraseCmd, sizeof(eraseCmd));
    To copy to clipboard, switch view to plain text mode 

    But it is not working when I want to add in the frame a counter as an idFrame;
    For example, if I send the 499th frame, I would like to send :
    {beginning if the frame....,0x01, 0xF3,...end of the frame }.

    I try a lot of functions, but maybe not the good one, like setNum(idFrame,16) but it returns :
    Frame[X] = 3 ;
    Frame[X+1] = F ;
    Frame[X+2] = 1 ;

    The best way for me will be to get :
    Frame[X] = "0x01" ;
    Frame[X+1] = "0xF3" ;

    Can someone help me ?
    Thank you in advance for your support.

    Zidoune

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: quint16 to QByteArray Conversion

    Something like this (frame_no is 2 byte unsigned integer) :
    Qt Code:
    1. Frame[X+2] = frame_no % 0xFF ;//lower byte
    2. Frame[X+1] = frame_no / 0xFF ;//higher byte
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2013
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: quint16 to QByteArray Conversion

    Thank you for your help Lesiok !

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: quint16 to QByteArray Conversion

    You need to understand the difference between the one byte number 0xF3 (243 in decimal) and the 5 byte string "0xF3" or to understand why setNum() was not the correct approach.

Similar Threads

  1. [SOLVED] quint16 weirdness
    By pdoria in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2009, 00:09
  2. quint16 compiling problem
    By MarkoSan in forum Qt Programming
    Replies: 5
    Last Post: 30th November 2007, 18:08
  3. Typedef quint16 on ARM and Win32
    By sgrasic in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 16th May 2006, 22:27
  4. How to convert from QString to quint16 ?
    By probine in forum Qt Programming
    Replies: 5
    Last Post: 31st March 2006, 09:00
  5. quint16 problem !!!
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 22nd March 2006, 22:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.