how to display a number that is a char

孤街醉人 提交于 2021-01-27 21:03:43

问题


I have a multi-dimensional array of chars that I want to display. one of the dimensions has numbers in it (0, 1, 2, etc.). When I go to display the array, I get the ascii results. I realize the char output works as defined (char + number = ascii) but I was looking to specifically show the number.

Ex.

char a = 3;

cout << a; // gives me #

I want to display 3. I have tried casting to an int: cout << (int)a;

I have tried casting inside the array myArray[(int)a];

Neither of those have seemed to work, and you can't convert const char to a string so I'm kinda lost. Any help will be appreciated.


回答1:


Assuming you have

char a = 3;

Now you can:

std::cout << static_cast<int>(a);

or

int b = a;
std::cout << b;

or

printf("%d",a);

The output of any of the above would be

3


来源:https://stackoverflow.com/questions/11471011/how-to-display-a-number-that-is-a-char

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!