Thread: Int to Char

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    Int to Char

    Is there an easy way to convert an integer to a character?


    PHP Code:
    strcat(myString,  char(myInteger)); 
    bruteRSsid\bruteRSsid.cpp(33) : error C2664: 'strcat' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Sure, assuming you mean, convert a single digit from 0..9 to a single character from '0'..'9'. Just add '0'.
    Code:
    char num_to_char(int num) {
        return num + '0';
    }
    However, the way you're passing it to strcat makes me think that you're converting more than one digit into more than one character. In that case, see: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    On the other hand, perhaps your problem is a combination of the above. Do you want to convert a single digit to a single character, and then append that to a string? Try this.
    Code:
    char string[10] = "foo";
    int x = 3;
    size_t len = strlen(string);
    
    string[len] = x + '0';
    len ++;
    string[len] = 0;  /* add NULL terminator */
    
    /* string is now: foo3 */
    I like to assign the return value of strlen() to a size_t, but ints work too.
    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.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    As you are working with std::strings, stay clear of C-style char array functions.

    To convert a number to a string, stringstreams are useful.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I didn't notice that myString was a std::string since it was being used as a parameter to strcat(). Oops.

    The FAQ I linked to covers stringstreams.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03: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, 08:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM