Solved

convert char to int

Posted on 2004-08-23
16
400 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
[X]
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
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

624 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