问题
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