Results 1 to 4 of 4

Thread: newbie - int to char* and more.

  1. #1
    Join Date
    May 2015
    Posts
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Exclamation newbie - int to char* and more.

    Hello,

    I am new to QT on Linux. I've been a .NET programmer and now working on Beaglebone Black. I'm using C++ to communicate with a motor controller which requires a command's parameter to be sent one byte at a time.

    My function receives an int (decimal) and this needs to be converted to Hex and then sent to another function one byte at a time.
    For example,

    Function parameter -> 16000 (decimal)
    convert it to Hex -> 3E80
    format the number as 000000 -> 003E80
    now this number must be sent one byte at a time so it must be broken down like this:
    char x[3];
    x[0] = 0x00;
    x[1] = 0x3E;
    x[2] = 0x80;

    How can I achieve this? Sorry I'm a real newbie in C++/QT.

    Thanks

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: newbie - int to char* and more.

    This is C++ 101. You should be able to use a union for this:

    Qt Code:
    1. union myConverter
    2. {
    3. std::uint32_t intVal;
    4. unsigned char[4] charVal;
    5. }
    6.  
    7. myConverter c;
    8. c.intVal = 16000;
    9.  
    10. // c.charVal[0] - [3] now contains the unsigned hex values
    To copy to clipboard, switch view to plain text mode 

    Of course, this assumes you have the same byte order on your target device. If not, you'll need to swap bytes around.

  3. #3
    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: newbie - int to char* and more.

    Or something like this
    Qt Code:
    1. quint32 value = 16000; // or 0x00003e80
    2. char x[3];
    3. x[0] = (value >> 16) & 0xff ;
    4. x[1] = (value >> 8) & 0xff ;
    5. x[2] = value & 0xff ;
    To copy to clipboard, switch view to plain text mode 

    The question really has nothing to do with hex, which is a human friendly string representation of a binary value.

  4. #4
    Join Date
    May 2015
    Posts
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Thumbs up Re: newbie - int to char* and more.

    Both the solutions worked. thanks experts!!

Similar Threads

  1. Char Followed by a Char is illegal (qglobal.h)
    By saad_saadi in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2014, 13:19
  2. a newbie..
    By Lady Sophia in forum General Discussion
    Replies: 3
    Last Post: 1st May 2012, 00:15
  3. How to convert unsigned char[] to char *?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 29th April 2011, 08:58
  4. char to const char* with atof
    By mickey in forum General Programming
    Replies: 5
    Last Post: 29th February 2008, 04:10
  5. Conversion from unsigned char* to unsigned char
    By santosh.kumar in forum General Programming
    Replies: 1
    Last Post: 6th August 2007, 13:12

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.