How do i display the ASCII code for a character?
Printable View
How do i display the ASCII code for a character?
Try:
If you cast a char to an int, it works fine. Note that it displays a 0 as the last character.Code:char *pcMyString = "Hello World";
char *p = pcMyString;
while(*(p++)) {
cout << (int)(*p) << endl;
}
For some reason when i use (int) it returns
-6
every time.
That's funny - it works on mine :(.
Mine outputs:
Code:101
108
108
111
32
87
111
114
108
100
0
What happened to your 'H', parksie?
Unnkkgghhh...
Sorry - I wasn't thinking straight:
Code:char *pcMyString = "Hello World";
char *p = pcMyString;
while(*p) {
cout << *p << " : " << (int)(*p) << endl;
p++;
}