Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 404
  • Last Modified:

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
0
Samooramad
Asked:
Samooramad
  • 8
  • 6
  • 2
2 Solutions
 
CEHJCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
objectsCommented:
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now