• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

convert character to integer

 
Ranch Hand
Posts: 97
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai,
how can i convert a character to integer.
and
integer to character.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by casting using (char) and (int).
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Java in General (Beginner).
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah you can do like:


then print it..

justin
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I convert Charater to Integer and vice versa as well... using JFrame? can anyone help me?
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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/
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot guys...
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic