aspose file tools*
The moose likes Beginning Java and the fly likes convert  character  to  integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "convert  character  to  integer" Watch "convert  character  to  integer" New topic
Author

convert character to integer

ramchander yshetti
Ranch Hand

Joined: Apr 22, 2006
Posts: 97
hai,
how can i convert a character to integer.
and
integer to character.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Start by casting using (char) and (int).
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Moving to Java in General (Beginner).


[Jess in Action][AskingGoodQuestions]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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;


You down with OOP? Yeah you know me!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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 ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
yeah you can do like:


then print it..

justin
jake barrozo
Greenhorn

Joined: Jul 10, 2009
Posts: 2
How can I convert Charater to Integer and vice versa as well... using JFrame? can anyone help me?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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/
cchetan jain
Ranch Hand

Joined: Jul 05, 2009
Posts: 30
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;
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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.
jake barrozo
Greenhorn

Joined: Jul 10, 2009
Posts: 2
Thanks a lot guys...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
You're welcome
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: convert character to integer
 
Similar Threads
Trouble with system.in manipulation
JAVA IDE (BEGINNERS)
files
use of wrapper class
Unicode Character Values