Output ASCII value of character

前端 未结 4 998
一整个雨季
一整个雨季 2020-12-20 10:39
#include 

using namespace std;

int main()
{
    char x;
    cout << \"enter a character:\";
    cin >> x;
    cout << \"ASCII Val         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-20 11:35

    std::cout << "ASCII Value of " << x << "is" << (int)x;
    

    is one way (the cast circumvents the special treatement of a char type by the I/O stream library), but this will output your platform's encoded value of the character, which is not necessarily ASCII.

    A portable solution is much more complex: You'll need to encode the ASCII set in a 128 element array of elements capable of storing a 7 bit unsigned value, and map x to a suitable element of that.

提交回复
热议问题