This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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: 32654
4
posted
0
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
posted
0
yeah you can do like:
then print it..
justin
jake barrozo
Greenhorn
Joined: Jul 10, 2009
Posts: 2
posted
0
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
posted
0
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;
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;
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
posted
0
Thanks a lot guys...
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
0
You're welcome
Fred Hamilton
Ranch Hand
Joined: May 13, 2009
Posts: 679
posted
0
Campbell Ritchie wrote:Welcome to JavaRanch Jaka Barrozo.
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.
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.