Results 1 to 3 of 3

Thread: How to get ASCII Value

  1. #1
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: How to get ASCII Value

    Hi,

    I use Qt C++ 5.4
    Docs says I use toLatin1 instead of toAscii.
    I want Ascii value of each character eg. A = 65, B=66, C=67

    QString str1;
    str1 ="ABCDEF"

    for (int i=0; i< str1.size(); i++)
    qDebug() << i << str1.mid(i,1).toLatin1() << str1.mid(i,1).toUtf8() << str.at(i).toLatin1() ;
    But result is show only Letters not Ascii Value

    0 "A" "A" A
    1 "B" "B" B
    2 "C" "C" C
    3 "D" "D" D

    How can I get each character's US-ASCII value of string?
    I want my result as follow;
    0 65
    1 66
    2 66
    3 67


    Thanks


    Added after 6 minutes:


    I found the solution now!.

    QChar ch;
    for(int i=0;i< str1.size();i++)
    {
    ch= s.at(i);
    qDebug() << ch.unicode();
    }
    Last edited by binary001; 16th October 2015 at 12:03.

  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: How to get ASCII Value

    toLatin1(), toLocal8Bit() and toUtf8() create the 8-bit encoded form of the string, using the respective text codec.

    For a string that only contains 7-bit ASCII characters this is usually exactly the same, at least toLatin1() and toUtf8() are.

    Cheers,
    -

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

    binary001 (17th October 2015)

  4. #3
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get ASCII Value

    Solution is OK. Notes:
    (1) s.at() is already a QChar, you do not need declare ch.
    Qt Code:
    1. qDebug << s.at(i).unicode();
    To copy to clipboard, switch view to plain text mode 
    (2) The unicode() returns the unicode ordinal of the character. For ASCII characters (0x00 - 0x7F) the ordinal is the same as the "ASCII value". For other characters, the ordinal does not equal to any "code page" value and uses its own schema.
    (3) Note that the unicode() returns short, therefore, the Qt unicode implementation is UTF16 in fact. The values returned by unicode() will be okay for ordinals less than 0xFFFF (all two- and three-character sequences).

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

    binary001 (17th October 2015)

Similar Threads

  1. Hex to Ascii and Ascii to Hex Problem
    By havoc in forum Newbie
    Replies: 6
    Last Post: 20th July 2013, 23:24
  2. ASCII to int and int to ASCII
    By askbapi in forum Newbie
    Replies: 2
    Last Post: 31st March 2010, 22:36
  3. ASCII to PDF
    By jaca in forum Newbie
    Replies: 3
    Last Post: 19th September 2009, 23:51
  4. get ASCII equivalent data
    By navi1084 in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2009, 04:13
  5. Ascii problem
    By eekhoorn12 in forum Qt Programming
    Replies: 5
    Last Post: 17th October 2007, 13:59

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.