View previous topic :: View next topic |
Author |
Message |
pop
Joined: 18 Sep 2004 Posts: 22
|
How do you typecast char to int? |
Posted: Sat Sep 18, 2004 12:21 pm |
|
|
How do you typecast char to int? I am trying to convert any ascii character (e.g. letters, symbols, numbers etc.) to its integer equivalent, so that I can do some bit-masking operations. For example, I would like to convert letter 'z' to it's ascii integer equivalent of 122.
I know that in c++, this is easily done by something like the following:
int temp;
char myChar='z';
temp=(int) myChar;
However, CCS complains with following error: "A numeric expression must appear here".
My compiler version is 3.112.
Thanks |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Sat Sep 18, 2004 1:26 pm |
|
|
That ought to work. I don't know why you are getting an error. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Sep 18, 2004 1:28 pm |
|
|
To me your example code looks OK. I compiled it in PCH v3.187 and it compiles fine.
The only reasons I can think of for it not working with you are:
1) Your v3.112 compiler is very old and it may be a bug in this version.
2) The bug reporting system of the CCS compiler is lousy. Often another bug in the previous line is causing the compiler to go crazy. Check for missing ';' etc in the preceding line. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sat Sep 18, 2004 1:35 pm |
|
|
Just so you know, char and int are the same in CCS. Not true with all compilers but for CCS they are the same. You shouldn't need to convert them at all when using CCS. |
|
|
pop
Joined: 18 Sep 2004 Posts: 22
|
|
Posted: Sat Sep 18, 2004 2:20 pm |
|
|
I've tried your suggestions, but I still receive the same compiler error.
I have also tried to directly bit-mask a char with an int (e.g. following Mark's recommendation), but that just produced the same compiler error as before.
I guess I'll just have to re-design my code around this problem, but it's good to know that the code is syntactically correct and works on other versions of ccs compilers.
Thanks for the swift help. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sat Sep 18, 2004 5:07 pm |
|
|
Maybe you should post a small test program that demonstrates the problem. |
|
|
pop
Joined: 18 Sep 2004 Posts: 22
|
|
Posted: Sun Sep 19, 2004 6:06 pm |
|
|
It seems like it was a bad build.
I deleted all the project files, re-created the project, compiled and it worked like a charm.
Thanks. |
|
|
Guest
|
|
Posted: Mon Sep 20, 2004 2:14 pm |
|
|
frankly, I ran into a few problems like this:
A good running code won't compile under the same build CCS compiler.
All I do is to delete everything, except the .c .h files, and recreate the project, and everything works fine after that. |
|
|
|