PDA

View Full Version : How to get ASCII Value



binary001
16th October 2015, 12:03
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();
}

anda_skoa
16th October 2015, 14:17
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,
-

Radek
16th October 2015, 19:16
Solution is OK. Notes:
(1) s.at() is already a QChar, you do not need declare ch.


qDebug << s.at(i).unicode();

(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).