Int to char conversion
-
wrote on 24 Jan 2013, 12:17 last edited by
The Ascii value of the characters should get the related character. I am searching for any function which does that, example for i=65, I get 'A'.
-
wrote on 24 Jan 2013, 12:29 last edited by
I think it can be done with a cast.
Something like that:char a = (char)i;
Hope it works.
Regards.
-
wrote on 24 Jan 2013, 12:57 last edited by
Look into using [[doc:QChar]].
-
wrote on 24 Jan 2013, 13:10 last edited by
[quote author="BelenMuñoz" date="1359030558"]
char a = (char)i;
[/quote]This is a C style cast and in C++ code it is high recommend to use static_cast. Example:
@
int nMyInt = 65;
char nMyChar = static_cast<char>(nMyInt);
QChar char(nMyChar);
@ -
wrote on 24 Jan 2013, 13:12 last edited by
As QChar has a constructor that takes an int, the intermediary cast to char is not needed.
-
wrote on 24 Jan 2013, 13:17 last edited by
[quote author="Andre" date="1359033121"]As QChar has a constructor that takes an int, the intermediary cast to char is not needed.[/quote]
Yes, of course. I just wanted to highlight the recommended usage of static_cast instead of the C style cast :)
-
wrote on 24 Jan 2013, 13:19 last edited by
I'd say that avoiding the cast altogether is preferable to a static_cast, which is preferable over a C style cast :-)
-
wrote on 24 Jan 2013, 13:21 last edited by
[quote author="Andre" date="1359033597"]I'd say that avoiding the cast altogether is preferable to a static_cast, which is preferable over a C style cast :-)[/quote]
(y) Absolutely :)
-
wrote on 24 Jan 2013, 14:54 last edited by
[quote author="leon.anavi" date="1359033022"]
[quote author="BelenMuñoz" date="1359030558"]
char a = (char)i;
[/quote]This is a C style cast and in C++ code it is high recommend to use static_cast. Example:
@
int nMyInt = 65;
char nMyChar = static_cast<char>(nMyInt);
QChar char(nMyChar);
@
[/quote]Thanks for the reply, I still have bad habbits from C.
6/9