Results 1 to 3 of 3

Thread: Howq to get the ascii of a char ?

  1. #1
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Howq to get the ascii of a char ?

    Hi everybody,

    I have an integer value and I need to convert each integer digit value to an ascii code. For example if I have :
    Qt Code:
    1. int intValue = -123;
    To copy to clipboard, switch view to plain text mode 

    i whish to have the following string :
    Qt Code:
    1. QString asciiValue = "2D 31 32 33"
    To copy to clipboard, switch view to plain text mode 

    I is it possible to get the ascii code of a char ?

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    Qt Code:
    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. char x;
    6.  
    7. cout << "Enter character:\n";
    8. cin >> x;
    9. cout << "ASCII code for the letter is " << int(x) << ".\n";
    10. return 0;
    11. }
    To copy to clipboard, switch view to plain text mode 

    use ascii() to get the array of characters.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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 21:11.

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. char * problem
    By eleanor in forum Qt Programming
    Replies: 1
    Last Post: 5th July 2008, 14:06
  3. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 20: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.