Solved

convert char to int

Posted on 2004-08-23
16
397 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 67
How to fix  socket closed error 11 64
convert Absolute path to relative path of FTP location 7 51
JVM error from eclipse 1 27
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

751 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