Is there anyway of converting a char to it's ascii value other than specifiy that a=97, b=98 etc?
Is there anyway of converting a char to it's ascii value other than specifiy that a=97, b=98 etc?
char mychar = 'a';
int myvalue = static_cast<int>(mychar);
or
int myvalue = static_cast<int>('a');
Regards,
Mario Figueiredo
Using Borland C++ Builder 5
Read the Tao of Programming
This advise was brought to you by the Comitee for a Service Packless World
Use static_cast<int>(yourChar).
-----
char myChar = 'a';
int numRep = static_cast<int>(myChar);
-----
Kuphryn
thanx for that Just something else...what if it were a string. Would I be able to do the same or would I need to read each char and then convert it?
Sorry....do I need a specific .h file to use static_cast??
Use the same approach.
Kuphryn
Nope.Originally posted by Unregistered
Sorry....do I need a specific .h file to use static_cast??
If it's not compiling due to an identifier error, check your project. If you are under C, casts are done differently.
(type) expression;
instead of
static_cast<type>(expression)
so,
(int) 'a'; //for C
static_cast<int>('a'); //for C++
Regards,
Mario Figueiredo
Using Borland C++ Builder 5
Read the Tao of Programming
This advise was brought to you by the Comitee for a Service Packless World