Qt: Get ASCII Code From QChar
Asked Answered
T

1

13

I need to get the ASCII code from a QChar.

In Qt 5.2 QChar::ToAscii has been removed.

Here is my code. How can I get the ASCII code?

QString data;
int key;
key = data.at(i);
Torgerson answered 30/12, 2013 at 14:53 Comment(1)
Strange, seems that you had been able to provide an answer on your own, by casting the extracted char to an integer. This should accomplish your task without passing through the toAscii() function, in fact. Can you provide a deeper explanation about the problem you're facing, with that code?Replete
D
18

Use:

char QChar::toLatin1() const

From the doc:

Returns the Latin-1 character equivalent to the QChar, or 0. This is mainly useful for non-internationalized software.

From a Qt5.0 version

char QChar::toAscii() const

This function is deprecated. Returns the Latin-1 character value of the QChar, or 0 if the character is not representable.

Example

QString test("test");
QChar c = test.at(0);
int v_latin = c.toLatin1();
int v_ascii = c.toAscii();
qDebug() << v_latin << " " << v_ascii;

Output:

116   116
Donatist answered 30/12, 2013 at 15:59 Comment(1)
I am looking ascii code from × multiply sign and the QChar store a negative value = -41. When I look into the ascii table the value for it sign is 158, why do I get this negative value?Tantalize

© 2022 - 2024 — McMap. All rights reserved.