Results 1 to 5 of 5

Thread: [RESOLVED] 1 is a number or a character.

  1. #1

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Resolved [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

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: 1 is a number or a character.

    Characters are numbers. 49 is the ASCII code for '1'.

  3. #3

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    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

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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++.

  5. #5

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    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
  •  



Click Here to Expand Forum to Full Width