Advertisement

Ascii values of chars ?

Started by July 28, 2000 01:34 AM
6 comments, last by Tornado 24 years, 4 months ago
Just wondering... How can I get the ascii value of a character with VC++ ? The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
char value = 'A'; // value == 65

char array[] = { "A Sucks" }; // array[0] == 65

??



Edited by - Claus Hansen Ries on July 28, 2000 2:44:36 AM
Ries
Advertisement
As a matter of interest \0 will set the value of a certain byte to 0. Can I used any number up to 256 with this, like \65 to make an A?
Chris Brodie
the ''\'' character is the escape character, it stops the following character as being taken literally, for example:

\n - newline
\t - tab
\r - carriage return.
\\ - backslash
\" - Quote

\65 would escape the 6, but not the 5. 6 is not a valid escape character, so you would end up with unpredictable results.

When it comes to characters they''re pretty much the equivilant to a short int. If you want to set a character you can do it in one of three ways:

char value = ''A'';
char value = 65;
char value = (char)65;

And to answer the original question to display the ascii value of a character using printf:

char value = ''A'';
printf("Ascii value: %d",&value); // Prints out 65
printf("Char: %c",&value); // Prints out A

printf is strongly typed so you specify your format.
I think with cout, you would have to cast the char to an int, but I''m not sure of that, something like:
cout << (int)value;

Hope this helps.
"We who cut mere stones must always be envisioning cathedrals"
It worked !
Here''s what I did: I wanted to convert a character to it''s ascii value, so all I did is:
char letter;
letter = ''A''; // For example
return(((int)letter)-32)
And everything looks great.
Thanks for the help


The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
I may be missing something, but why did you subtract 32?
Advertisement
That''s because I made a font bitmap (With the Bitmap Font Builder) that made the rows and cols of the font as 16x8 (Bitmap size is 512x512).
So, to get to the right letter I needed to substract 32.

The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
quote: Original post by gimp

As a matter of interest \0 will set the value of a certain byte to 0. Can I used any number up to 256 with this, like \65 to make an A?


no, but you can make
char myChar = 65;

This topic is closed to new replies.

Advertisement