A friendly place for programming greenhorns!
convert character to integer
(1 like)
hai,
how can i convert a character to integer.
and
integer to character.
Start by casting using (char) and (int).
Moving to Java in General (Beginner).
well, to get a int from a char...

say you have..

now if you have single digit int..

you could just do this for int to char;

char a = '102';
int b = a.getNumericValue();

That won't work.
You want:-
You ought to get 15 (=f) back from that code snippet.
[ July 18, 2006: Message edited by: Campbell Ritchie ]

convert a character to integer?

But char values already are integers. You can do arithmetic with them. Try subtracting '0' from another numeric digit and see what prints out.
What a char is is an unsigned integer in the range 0 to 65535 (I think; it's 0xffff) which prints out as the Unicode character corresponding to that number, assuming your computer supports Unicode. The values 0 to 127 (0x7f) correspond to Unicode's original precursor (ASCII) and 0 to 255 (0xff) is extended ASCII.
yeah you can do like:

then print it..

justin
How can I convert Charater to Integer and vice versa as well... using JFrame? can anyone help me?
ok, I guess there are a few different ways to answer your question, heres another

suppose your character is between '0' and '9'

notice that the "decimal" ASCII code for '0' is 48 and for '9' it is 57. ( pardon my terminology use of 'decimal' if it is wrong ).

so if you have a primitive variable of type char, and it is somewhere between

char c = '0' and char c = '9', then to deal with the corresponding integer you subtract 48.

e.g. if char c = '5'; then the expression (c - 48) == 5 returns true;

http://www.ascii-code.com/
hi friends..
class A
{
public static void main(String ar[])
{
char c='a';
int i=c;
System.out.println(Character.getNumericValue(c));
System.out.println(i);
}
}
i did not get a point..why there is different values on the console for char c;
Welcome to JavaRanch Jaka Barrozo.

It's not always a good idea to wake a 3-yr old thread.

What people forget is that a char is a number, not a character. When you write char c = '5'; you do not store a 5 anywhere; you store 53 or more precisely 0000_0000_0011_0101. You don't convert chars to integers at all; chars always are integers (see this Java™ Language Specification section). You convert a character to an integer when you say

char c = '5'

and when you print it out

System.out.println(c);

you are converting an integer to a character. As Justin Fox and Fred Hamilton have shown, you can do arithmetic on chars, but usually you only get sensible answers with addition or subtraction.
Thanks a lot guys...
You're welcome

Campbell Ritchie wrote:Welcome to JavaRanch Jaka Barrozo.

It's not always a good idea to wake a 3-yr old thread.

What people forget is that a char is a number, not a character. When you write char c = '5'; you do not store a 5 anywhere; you store 53 or more precisely 0000_0000_0011_0101. You don't convert chars to integers at all; chars always are integers (see this Java™ Language Specification section). You convert a character to an integer when you say

char c = '5'

and when you print it out

System.out.println(c);

you are converting an integer to a character. As Justin Fox and Fred Hamilton have shown, you can do arithmetic on chars, but usually you only get sensible answers with addition or subtraction.

wow, three years old, didn't notice that. I was responding to a three year old question.

Anyways, we are getting into some semantic issues here when we speak of conversion

My example was meant as arithemetic, but it was arithemetic to do a conversion, in use if not in type, in the following sense...

Chess notation is often represented in a string. If I want to parse a character in that string as a row or column co-ordinate in order to access the corresponding array element, well then that char '5' needs to become an int 5, so I subtract 48 to do this. Not saying it's the best strategy, just a method I adopted some time back when I was learning, and I haven't got around to investigation any better options, yet.
(1 like)
String nString = "5";
char n = nString.charAt(0);
System.out.println(n - '0');
System.out.println(n - 48);
System.out.println(Character.getNumericValue(n));
System.out.println(new Integer(nString));
System.out.println(Integer.valueOf(nString));
System.out.println(Integer.parseInt(nString));
System.out.println(new BigInteger(nString));

And there are bound to be many other ways to do it. Note some of those methods may be inefficient in performance terms. Most people would start by trying Integer.parseInt.
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss

This thread has been viewed 364023 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 16, 2018 06:54:35.