Convert char to int in C and C++

前端 未结 12 1980
梦毁少年i
梦毁少年i 2020-11-22 02:41

How do I convert a char to an int in C and C++?

12条回答
  •  不知归路
    2020-11-22 02:59

    Use static_cast:

    int num = static_cast(letter); // if letter='a', num=97
    

    Edit: You probably should try to avoid to use (int)

    int num = (int) letter;

    check out Why use static_cast(x) instead of (int)x? for more info.

提交回复
热议问题