|
-
Jun 10th, 2007, 10:23 PM
#1
Thread Starter
PowerPoster
[RESOLVED] 1 is a number or a character.
Just look the following code fragment.
Code:
Console.WriteLine("The number " + '1' + " as a character have int value " +(int)'1' + ".");
Console.WriteLine("The number " + “1” + " as a number have int value " + (int)(1) + ".");
The output is,
Code:
The number 1 as a character has int value 49.
The number 1 as a number has int value 1.
I never use a number as character and got this by mistake one of my application.
What I want to know what, if I use a number (integer value) as a character it holds another integer value. What is the meaning/purpose of that?
Just forget about the casting on code line 2. I put it there to keep similarities between two code lines.
Last edited by eranga262154; Jun 10th, 2007 at 10:24 PM.
Reason: Incorrect
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Jun 10th, 2007, 10:43 PM
#2
Re: 1 is a number or a character.
Characters are numbers. 49 is the ASCII code for '1'.
-
Jun 10th, 2007, 10:49 PM
#3
Thread Starter
PowerPoster
Re: 1 is a number or a character.
Ya it is true. But if I'm use a number 1, simply as a character, it hold another int value, 49. Why is that.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Jun 10th, 2007, 10:54 PM
#4
Re: 1 is a number or a character.
Because all characters are stored as ASCII and if you cast a char to a numeric type you get its ASCII code.
It works the other way too:
Code:
char c = (char)49;
Console.WriteLine("I has a value: " + c);
By the way, that looks like C#, not C or C++.
-
Jun 10th, 2007, 11:00 PM
#5
Thread Starter
PowerPoster
Re: 1 is a number or a character.
Thanks, now I got that where I'm going wrong.
By the way, I put this on a wrong place. If you can move it to the right place.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|