aspose file tools*
The moose likes Beginning Java and the fly likes Integer to Char output question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Integer to Char output question" Watch "Integer to Char output question" New topic
Author

Integer to Char output question

Markus Zuckerberg
Greenhorn

Joined: Sep 25, 2011
Posts: 2
Hi !

This is my first post here!

I'm java beginner and have a simple question:

I initialize an integer calc = (char)100 +(char)12; the integer sum output would be 112, but I'd like to print out the ascii-char ("p") of the output of 112 and not the integer sum of 112.

how can i do that?


thanks in advance,

regards
markus
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Casting has a higher precedence than addition, so you should explicitly make sure the addition is performed before the cast:
Markus Zuckerberg
Greenhorn

Joined: Sep 25, 2011
Posts: 2
Stephan van Hulst wrote:Casting has a higher precedence than addition, so you should explicitly make sure the addition is performed before the cast:


Thanks, but I'd like to convert a char into another char with an addition like:

char a = 'P';
char b = a + (char)32;

print out... char b

I thought the char P is stored as an integer value and i can calculate with this value, add another value and give it back/out as an ascii-char?

Thanks in advance,
markus


Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Yes, you can do that, but addition automatically promotes its operands to at least an int. So what you need to do is the following:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integer to Char output question