Results 1 to 3 of 3

Thread: Convert int to hex and add to char array to send to serial port

  1. #1
    Join Date
    Jul 2015
    Posts
    18
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Convert int to hex and add to char array to send to serial port

    Hello everyone, I'm sorry if this had been asked but I couldn't find anything in the forum like this.

    So let's say I have an array of char like this:

    Qt Code:
    1. char thd[] = {0x01, 0x07, 0x02, value, 0x00, 0x6E, 0x04}
    To copy to clipboard, switch view to plain text mode 

    I make a Qlineedit that gets user input of an integer from 0 -> 255. For instance, the user will input 100.

    How do I take that 100 and turn it into 0x64 and put that into the place of "value" to send the char array to the serial port. I've tried QString::number to convert it to int but I get stuck there because I can't figure out how to add the "0x" in front of it and add it to the array.

    I will appreciate any attempt of helping. Thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Convert int to hex and add to char array to send to serial port

    Quote Originally Posted by hovuquocan1997 View Post
    I've tried QString::number to convert it
    You mean QString::toInt()?

    Quote Originally Posted by hovuquocan1997 View Post
    I can't figure out how to add the "0x" in front of it
    You don't need to do that. The 0x is only telling the compiler that the following sequence of characters is to be intereted as a hex number (integer number with base 16).

    Quote Originally Posted by hovuquocan1997 View Post
    and add it to the array.
    If I understand your array correctly, you want to write the value to the fourth position.
    Arrays allow direct access to all their fields by numerical index, starting at 0. The fourth field is therefore thd[3]

  3. The following user says thank you to anda_skoa for this useful post:

    hovuquocan1997 (15th July 2015)

  4. #3
    Join Date
    Jul 2015
    Posts
    18
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Convert int to hex and add to char array to send to serial port

    Yes sorry, I meant to say QString::toInt, and thank you, I figure out my problem. I initialized a QString type for QString::toInt instead of an integer type and that's why I couldn't add it to the char array. And I always thought that I needed to add the "0x" in front and therefore it didn't work. This thread can be marked as solved now .

Similar Threads

  1. Replies: 2
    Last Post: 15th March 2014, 12:54
  2. Replies: 2
    Last Post: 3rd July 2012, 17:42
  3. Replies: 5
    Last Post: 27th May 2011, 10:38
  4. send binary file over serial port with QSerialDevice
    By ilpaso in forum Qt Programming
    Replies: 30
    Last Post: 13th December 2010, 13:08
  5. Replies: 6
    Last Post: 16th July 2010, 12:07

Tags for this Thread

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.