Results 1 to 7 of 7

Thread: Convert int to hex and placing it in an array of char's

  1. #1
    Join Date
    May 2010
    Posts
    5
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Convert int to hex and placing it in an array of char's

    Hello,

    I would like to convert an int number to hex and place it in an array of chars.

    Here's the code:
    Qt Code:
    1. void MainWindow::on_dial_valueChanged(int value)
    2. {
    3. qDebug() << value;
    4. // convert variable value to 0x(value), example value=99 -> new_value = 0x63
    5.  
    6. char value_to send[11] = {0x01, 0x09, 0x00, 0x13, 0x04, 0x03, 0x20, 0x01, new_value, 0x05, 0x00};
    7. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Convert int to hex and placing it in an array of char's

    Hi,

    simply use QString::number() with 16 as base.

  3. #3
    Join Date
    May 2010
    Posts
    5
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Convert int to hex and placing it in an array of char's

    Hello,

    but with QString I can't put the number in the char array.

    Qt Code:
    1. void MainWindow::on_dial_valueChanged(int value)
    2. {
    3. qDebug() << value;
    4. QString new_value = QString().number(value, 16).prepend("0x");
    5. qDebug() << new_value;
    6. const char value[11] = {0x01, 0x09, 0x00, 0x13, 0x04, 0x03, 0x20, 0x01, new_value, 0x05, 0x00}; // error: cannot convert ‘QString’ to ‘const char’ in initialization
    7. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Convert int to hex and placing it in an array of char's

    If you also can use const char * then better use QByteArray and transform it to char with data().

  5. The following user says thank you to Lykurg for this useful post:

    LDS (16th July 2010)

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Convert int to hex and placing it in an array of char's

    Modern computers store all numbers in hexadecimal form so there is no point in "converting an integer to hex" because it already is hex.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    LDS (16th July 2010)

  8. #6
    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: Convert int to hex and placing it in an array of char's

    LDS, you appear to be thinking of the new_value in:
    Qt Code:
    1. char value_to send[11] = {0x01, 0x09, 0x00, 0x13, 0x04, 0x03, 0x20, 0x01, new_value, 0x05, 0x00};
    To copy to clipboard, switch view to plain text mode 
    as if it were a pre-processor macro expansion rather than the run-time variable use it is. In pre-processing the macro is literally replaced with its value (as if you typed it there) and then the code is compiled: it is a compile time event. What you want is for the value passed in to the function to be placed in the array at run time. There is, as others have pointed out, no need to do anything to value other than check it is a valid character value.
    Qt Code:
    1. void MainWindow::on_dial_valueChanged(int value)
    2. {
    3. // check that value is in the range 0 to 255 (or -128 to 127) perhaps?
    4. char value_to send[11] = {0x01, 0x09, 0x00, 0x13, 0x04, 0x03, 0x20, 0x01, value, 0x05, 0x00};
    5. // do something with the array before it goes out of scope
    6. }
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to ChrisW67 for this useful post:

    LDS (16th July 2010)

  10. #7
    Join Date
    May 2010
    Posts
    5
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Convert int to hex and placing it in an array of char's

    Hello,

    yes, I was confusing both.
    I managed to get the "job" done.
    I did the math with the chars instead of converting to int's.
    Qt Code:
    1. ...
    2. char device_value[11] = {0x01, 0x09, 0x00, 0x13, 0x04, 0x03, 0x20, 0x01, 0x00, 0x05, 0xFF};;
    3. int size = sizeof(device_value);
    4.  
    5. device_value[8] = value;
    6.  
    7. result = 0xFF ^ device_value[1]; // first value
    8. for (i=2; i < 10; i++)
    9. {
    10. result ^= device_value[i];
    11. }
    12. qDebug("\nfirst checksum: %x\n", result);
    13.  
    14. device_value[10] = result;
    15. qDebug("value8=%x\n", device_value[8]);
    16. ...
    To copy to clipboard, switch view to plain text mode 

    Thanks everybody for helping me.
    This thread can be marked as solved.

    Best regards,
    LDS

Similar Threads

  1. Char array[6] to QString and display it
    By arunvv in forum Newbie
    Replies: 11
    Last Post: 12th March 2014, 20:48
  2. Replies: 4
    Last Post: 30th April 2013, 00:49
  3. Converting QString to char array
    By srohit24 in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2009, 18:19
  4. Conversion Char Array to string
    By anafor2004 in forum Newbie
    Replies: 6
    Last Post: 6th May 2008, 14:35
  5. Char Array to QString
    By 3nc31 in forum Qt Programming
    Replies: 2
    Last Post: 25th November 2007, 22:18

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.