Solved

convert char to int

Posted on 2004-08-23
16
356 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
Comment Utility
int i = c; // where 'c' is a char
0
 

Author Comment

by:Samooramad
Comment Utility
I need to compare them
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
// (if c == '1')
int i = (int)c - '0';
0
 

Author Comment

by:Samooramad
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
if (myclass.getPosition() == c)
{
   // equal
}
0
 

Author Comment

by:Samooramad
Comment Utility
sorry got cut off.
yes i meant i do want positive values.. that was a typo
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Samooramad
Comment Utility
objects dont you need to convert it?
0
 
LVL 86

Expert Comment

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

Expert Comment

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

Author Comment

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

Expert Comment

by:CEHJ
Comment Utility
>>sorry CEHJ. I didnt understand that was what you meant

Yes assignment/comparison address the same type conversion issues
0
 

Author Comment

by:Samooramad
Comment Utility
thanks objects
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 …
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…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now