Thread: how to transform integer to char..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    how to transform integer to char..

    how to transform integer to char ?
    int x=3;
    char y;

    i know that in order to do the opposite thing
    i need to
    Code:
    int x;
    char y='3';
    
    x=y-'0';

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    a 'char' is an unsigned /or/ signed integer of at least 8 bits,
    an 'int' is a signed integer of at least 16 bits (to be checked) so you can cast from one to the other with the expected loss from int to char...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, if the opposite is to subtract, then perhaps what you should do is to add. Of course, the assumption is that the integer is only one digit.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i can do casting??

    Code:
    int x=3;
    char y;
    y=(char)x;

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, e.g.,
    Code:
    int x = 3;
    char y;
    y = (char)(x + '0');
    You might want to assert(x >= 0 && x <= 9) since this is only supposed to work with x being in that range.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    i can do casting??
    Yes, casts work for all elementary types (with possible side effects).

    C-compiler Design Rules*:
    rule 1: you do not prevent the programmer from doing what [s]he wants
    rule 2: you DO NOT prevent the programmer from doing what [s]he wants

    *c99 rationale, introduction, 'Keep the spirit of C'
    Last edited by root4; 12-31-2008 at 10:59 AM.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    3
    Maybe you should take a look at the itoa and atoi functions

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by KroniskBakfylla
    Maybe you should take a look at the itoa and atoi functions
    atoi does not apply here and itoa is non-standard (but then there's the printf-family of functions).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. checking if a char * is an integer
    By gregulator in forum C Programming
    Replies: 5
    Last Post: 04-16-2004, 09:12 AM