convert char to int

Hi experts,
how do I do what I named the title of the question? :)
I know there is something like parseInt or something but I need exact syntax
SamooramadAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
int i = c; // where 'c' is a char
0
 
SamooramadAuthor Commented:
I need to compare them
0
 
CEHJCommented:
// (if c == '1')
int i = (int)c - '0';
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
SamooramadAuthor Commented:
what if I dont have a positive value for c to compare it with?
I need something liike:
if myclass.getPosition() == the character converted to integer
0
 
CEHJCommented:
It depends on how you're treating these chars but say the char is holding a representation of a digit:

char c = '1';
int x = 1;
int y = (int)c - '0';

System.out.println(x == y); // prints true
0
 
CEHJCommented:
>>what if I dont have a positive value for c to compare it with?

c couldn't hold a negative value. What values do you want it to hold?
0
 
objectsConnect With a Mentor Commented:
if (myclass.getPosition() == c)
{
   // equal
}
0
 
SamooramadAuthor Commented:
sorry got cut off.
yes i meant i do want positive values.. that was a typo
0
 
SamooramadAuthor Commented:
objects dont you need to convert it?
0
 
CEHJCommented:
You don't - as my first comment shows
0
 
CEHJCommented:
A char is a numeric type that just happens to be able to store 'characters'
0
 
SamooramadAuthor Commented:
sorry CEHJ. I didnt understand that was what you meant
thanks
0
 
CEHJCommented:
>>sorry CEHJ. I didnt understand that was what you meant

Yes assignment/comparison address the same type conversion issues
0
 
SamooramadAuthor Commented:
thanks objects
0
 
CEHJCommented:
8-)
0
 
objectsCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.