Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Int to char conversion



  • 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'.



  • I think it can be done with a cast.
    Something like that:

    char a = (char)i;

    Hope it works.

    Regards.



  • Look into using [[doc:QChar]].



  • [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);
    @



  • As QChar has a constructor that takes an int, the intermediary cast to char is not needed.



  • [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 :)



  • I'd say that avoiding the cast altogether is preferable to a static_cast, which is preferable over a C style cast :-)



  • [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 :)



  • [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.


Log in to reply