• 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

Char into Integer

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a variable "c" of type char, I need to convert that variable to an integer value. How can I do this?.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i made this:


but then last two lines left me quite perplexed...
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char is assignable to int



As simple as that.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, that's exactly what i did, but my Q remains: what means the output beeing 55?
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ASCII value.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by miguel lisboa:
ok, that's exactly what i did, but my Q remains: what means the output beeing 55?



This is the Unicode value for whatever character was stored in the variable. For the characters in the English alphabet, the Unicode value is the same as the ASCII value.

Layne
[ May 15, 2005: Message edited by: Layne Lund ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manuel Diaz:
Hi, I have a variable "c" of type char, I need to convert that variable to an integer value. How can I do this?.



As you can see above, it depends on what you mean by "the integer value". Do you want the Unicode value? Or do you want the numerical value corresponding to the char (i.e. 0 for '0', 1 for '1', etc.). As miguel's example shows, you can use the String and Integer classes to convert to the numerical value. However, there is a shorter way. Since char is internal stored as an integral value, you can use arithmetic operations. It also helps that all the digit characters are stored in order. This means that you can do something like this:

This takes the Unicode value of the input character and subtracts the Unicode value for the character '0'. It should be obvious that this will produce the correct numerical value for any digit character. If you really want to use this method, you should also add error handling code to make sure the char is REALLY a digit (and not a letter or punctuation or something lese).

Layne
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read this post, here is my problem explained, is my last reply:

https://coderanch.com/t/399682/java/java/do
My post
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The solution is quite simple:


Using the example from your other thread:

should get you what you want.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic