• 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 number to character of that value

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I convert say
int num = 65;
to a character value of "A"
that is the decimal 65 is the letter A in Ascii. I need to know how to force this conversion. I tried
char c;
c = (char)num;

the compiler refuses to do the convert. Note I do not want the digits 6 and 5, I want the letter A as output. Is this even possible with Java?

In basic you have the chr(65) function which returns the letter A. I'm looking for the same functionality. Ulitmately I want to use math.random to generate some random letters.

Thanks for help a newb.
 
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
Hi,

Welcome to JavaRanch!

What you've shown is perfectly fine -- don't know why you couldn't get it to work. If you show an actual code example that won't compile for you, we could tell you what the problem is with it. But

int i = 65;
char c = (char) i;

will work perfectly.
[ September 03, 2006: Message edited by: Ernest Friedman-Hill ]
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dude...Works fine for me in eclipse.

Here's the whole class:

class CharTest {
public static void main(String args[]) {
int num = 65;
char c;
c = (char)num;
System.out.println(c);
}
}

Cheers!
 
John Warner
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fellas, thanks, I found my error. I was trying to return a String from the function that I was doing the convert in. As soon as I added a Character.toString(myChar) everything fell into place. My real problem was thinking the compiler was not going to allow this at all rather then me nit understanding what had to be done. Once you both said 'hey this works' I started looking harder at my code.

Thanks again!
 
reply
    Bookmark Topic Watch Topic
  • New Topic