Solved

convert char to int

Posted on 2004-08-23
16
393 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
Technology Partners: 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 40
Why doesn't this text field show up on my Applet frame? 2 27
Eclipse Java import and method not resolved 4 73
Setup GlassFish 4 34
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

713 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