Solved

convert char to int

Posted on 2004-08-23
16
377 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simple java question 3 56
throw exception 21 44
Running JavaFX on JDeveloper 12C 1 33
Crystal Reports Licensing Questions 4 13
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

822 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