Results 1 to 3 of 3

Thread: Howq to get the ascii of a char ?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Howq to get the ascii of a char ?

    Maybe something like this:

    Qt Code:
    1. int val = 123;
    2. int rest;
    3. int digit;
    4. rest=val;
    5. QVector<char> v;
    6. while(rest>0){
    7. digit = rest % 10; // last digit
    8. rest = rest / 10;
    9. v.push_front(digit+'0');
    10. }
    To copy to clipboard, switch view to plain text mode 

    Then if you want their hex value, just sprintf them with "%x" formatting, like:

    Qt Code:
    1. for(QVector<char>::const_iterator it = v.begin(); it!=v.end(); ++it){
    2. ::snprintf("%x", *it);
    3. qDebug(qPrintable(hex));
    4. }
    To copy to clipboard, switch view to plain text mode 
    Of course you can use QString::arg() for that.

    You can also use QString methods directly like so:

    Qt Code:
    1. int val = 123;
    2. QString text = QString::number(val);
    3. for(int i=0;i<text.size();i++){
    4. hex << QString::number(text[i].toAscii(), 16);
    5. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 27th April 2006 at 22:11.

Similar Threads

  1. Char array[6] to QString and display it
    By arunvv in forum Newbie
    Replies: 11
    Last Post: 12th March 2014, 21:48
  2. char * problem
    By eleanor in forum Qt Programming
    Replies: 1
    Last Post: 5th July 2008, 15:06
  3. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 21:49

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
  •  
Qt is a trademark of The Qt Company.