Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

convert char to int

Posted on 2004-08-23
16
386 Views
Last Modified: 2010-03-31
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
Comment
Question by:Samooramad
  • 8
  • 6
  • 2
16 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 350 total points
ID: 11875667
int i = c; // where 'c' is a char
0
 

Author Comment

by:Samooramad
ID: 11875689
I need to compare them
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11875699
// (if c == '1')
int i = (int)c - '0';
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:Samooramad
ID: 11875735
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11875737
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11875757
>>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
 
LVL 92

Assisted Solution

by:objects
objects earned 150 total points
ID: 11875772
if (myclass.getPosition() == c)
{
   // equal
}
0
 

Author Comment

by:Samooramad
ID: 11875850
sorry got cut off.
yes i meant i do want positive values.. that was a typo
0
 

Author Comment

by:Samooramad
ID: 11875856
objects dont you need to convert it?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11875868
You don't - as my first comment shows
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11875876
A char is a numeric type that just happens to be able to store 'characters'
0
 

Author Comment

by:Samooramad
ID: 11875889
sorry CEHJ. I didnt understand that was what you meant
thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11875907
>>sorry CEHJ. I didnt understand that was what you meant

Yes assignment/comparison address the same type conversion issues
0
 

Author Comment

by:Samooramad
ID: 11875910
thanks objects
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11875941
8-)
0
 
LVL 92

Expert Comment

by:objects
ID: 11875997
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 104
Oracle SQL syntax check  without executing 6 74
difference of if loops 23 60
web project error add remove 1 27
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question