Thread: Convert integer to char..

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    46

    Convert integer to char..

    Hi..

    I am trying to convert an integer to a char.. How can I do this..?

    ex.
    int i = 7;
    char p[12] = "testint = i"
    p[10] = i; // HOW CAN I DO THIS
    p[11] = '\0'

    or

    int i = 7;
    char *p = "testint = "
    strcat(p,i) // DOES NOT SEEM TO WORK NEIGHTER

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    just cast it.
    e.g.
    int i = 7;
    char p[12] = "testint = i"
    p[10] = (char)i;
    ...

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Do you mean option 1 from here? http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Casting (int)7 to a character would give you ASCII character 7, which is the same as \a, I think. In other words, your computer would beep. If it's '7' you're looking for, just add '0'.
    Code:
    int number = 7;
    char character = number + '0';
    
    printf("%d %c\n", number, character);
    Output:
    Code:
    7 7
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    46
    Abda92.. Does not seem to work..

    PORTlastNumber = 7;
    PORT = 31000 + PORTlastNumber;
    //char *p="smrdemo -t 5";
    //char *s;
    //int j=4;
    char p[13]; //="smrdemo -t ";
    p[0] = 's'; p[1] = 'm'; p[2] = 'r';p[3] = 'd';p[4] = 'e';p[5] = 'm';p[6] = 'o';p[7] = ' ';p[8] = '-';p[9] = 't';p[10] = ' ';p[11] = '7';p[12] = '\0';

    //"smrdemo -t 7";
    //strcat(p,itoa(j,buff,10));
    //sprintf(s,"smrdemo -t %i",5);
    //printf("s --> %s \n",s);
    //printf("p --> %s \n",p);
    p[11] = (char) PORTlastNumber;//PORTlastNumber;
    printf("JJJJJ->%s \n",p);
    printf("FFFFF->%c \n",p[11]);
    Returns:
    JJJJJ->smrdemo -t
    FFFFF->

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    46

    Smile

    Quote Originally Posted by dwks View Post
    Do you mean option 1 from here? http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Casting (int)7 to a character would give you ASCII character 7, which is the same as \a, I think. In other words, your computer would beep. If it's '7' you're looking for, just add '0'.
    Code:
    int number = 7;
    char character = number + '0';
    
    printf("%d %c\n", number, character);
    Output:
    Code:
    7 7
    Yes... PERFECT... Thank you alot.. I works just fine.. You just saved rest of my evening... Thanks alot..

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Abda92's solution won't work, as you have seen. Try this.
    Code:
    p[11] = PORTlastNumber + '0';
    Also, instead of this:
    Code:
    char p[13]; //="smrdemo -t ";
    p[0] = 's'; p[1] = 'm'; p[2] = 'r';p[3] = 'd';  /* ... */
    Consider using
    Code:
    char p[13] = "smrdemo -t 7";
    The 13 size is also optional. If you leave it out, the compiler will count how many bytes are required for the string (including the NULL). This only works if you initialize the string like I have done.

    [edit] I see it's working now. Good luck. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    46
    Quote Originally Posted by dwks View Post
    Abda92's solution won't work, as you have seen. Try this.
    Code:
    p[11] = PORTlastNumber + '0';
    Also, instead of this:
    Code:
    char p[13]; //="smrdemo -t ";
    p[0] = 's'; p[1] = 'm'; p[2] = 'r';p[3] = 'd';  /* ... */
    Consider using
    Code:
    char p[13] = "smrdemo -t 7";
    The 13 size is also optional. If you leave it out, the compiler will count how many bytes are required for the string (including the NULL). This only works if you initialize the string like I have done.

    [edit] I see it's working now. Good luck. [/edit]
    I know about the char p[13]="smrdemo -t ";. It was just a desperate try to fix it.. And Thanks again..

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Sorry, I guess I understood the question wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. strcat - cannot convert char to const char
    By ulillillia in forum C Programming
    Replies: 14
    Last Post: 12-07-2006, 10:00 AM
  5. convert from an integer to an unsigned char
    By Landroid in forum C Programming
    Replies: 4
    Last Post: 05-02-2005, 01:43 AM