basic c++ question

Discussion in 'OT Technology' started by Joe_Cool, Sep 10, 2004.

  1. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,488
    Likes Received:
    616
    how do you store the value of an int variable in a string of characters?
     
  2. Scoob_13

    Scoob_13 Anything is possible, but the odds are astronomica

    Joined:
    Oct 5, 2001
    Messages:
    73,798
    Likes Received:
    38
    Location:
    Fort Worth. Hooray cowgirls.
    What exactly do you mean?

    How do you convert from characters to numbers? How do you convert from numbers to characters? How do you pull a set of characters out of a string and convert them to numbers?
     
  3. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,488
    Likes Received:
    616
    No, how do you take the value of an int and store it as a string.
    Example:
    int a=12345678;
    char b[25];
    I want b to contain '1', '2', '3', '4', '5', '6', '7', '8', '\0'.

    But I found sprintf(), so I think I answered my own question.
     
  4. TMReX

    TMReX Spoooooon!

    Joined:
    May 23, 2003
    Messages:
    50
    Likes Received:
    0
    Location:
    you better check under the sea! or Dallas
    Be careful with sprintf() use snprintf() instead. And be sure you check the return value in case your buffer was too small.
     
  5. EkriirkE

    EkriirkE Zika Xenu OT Supporter

    Joined:
    Jan 11, 2004
    Messages:
    14,799
    Likes Received:
    0
    Location:
    Dublin & San Francisco, CA
    itoa is a func specifically for that purpose (int to ascii)
     
  6. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,488
    Likes Received:
    616
    itoa isn't ANSI though. It's not in GCC. :dunno:

    :bigthumb: Thanks for the tip.
     
  7. EkriirkE

    EkriirkE Zika Xenu OT Supporter

    Joined:
    Jan 11, 2004
    Messages:
    14,799
    Likes Received:
    0
    Location:
    Dublin & San Francisco, CA
     
  8. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    strtol is in the SUS:

    long strtol(const char *restrict str, char **restrict endptr, int base);

    sprintf would be easier to use for your purposes though.
     
  9. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    If you're into the C++ standard library, there is also ostringstream;

    Code:
    #include <sstream>
    
    std::ostringstream os;
    
    os << 12345678;
    
    std::string str = os.str();
    // or
    char buf[10];
    strcpy(buf, os.str().c_str());
    
     
  10. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,488
    Likes Received:
    616

    I like that. :)
     

Share This Page