I need to convert a char to an int...

This is a discussion on I need to convert a char to an int... within the C++ Programming forums, part of the General Programming Boards category; Couldn't you do something like "num = (int)character;"?...

  1. #16
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Couldn't you do something like "num = (int)character;"?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #17
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Hunter2
    Couldn't you do something like "num = (int)character;"?
    He actually meant a string (char array), not a single character.
    Plus, that won't work so good since 5 != '5'
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, look at the code example above. That's not a char array! And I'm not sure what static_cast<int> does, but I'm pretty sure it's the same thing as (int) - (int) is typecasting, and static_cast is... well, staticcasting I guess What's the difference?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #19
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Hunter2
    Well, look at the code example above. That's not a char array!
    The original poster used cin.getline() to read the input, and that returns a char array, not a character.
    Using int = (int)char can only convert a number in the range 0-9, plus it wouldn't display right without some extra modification. Try to print out (int)'1' and you'll see why.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #20
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Sorry, I might have misunderstood... (int) converts it to the integer value of the ascii character?

    Using int = (int)char can only convert a number in the range 0-9, plus it wouldn't display right without some extra modification.
    Sorry, I don't follow you very well...

    Code:
    char character = 'a';
    int n = (int)character; //no error here!
    std::cout << n;
    Doesn't it print out the int value of the ascii character like it should? (i.e. 97 or something)

    P.S.
    I wasn't replying to the original poster; I was replying to Troll_King. I merely suggested the use of (int)character instead of static_cast<int>(character). I never said that I was right that it was/did the same thing, I simply made a statement that it looked the same to me.

    P.P.S.
    Well, look at the code example above. That's not a char array!
    the code above (i.e. Troll_King's code) has nothing to do with the original poster's code. Did you not read my post before hitting reply?
    Last edited by Hunter2; 10-17-2002 at 05:40 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  3. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21