Question on converting unsigned long to std::string

Discussion in 'C++' started by Ramesh, Oct 10, 2008.

  1. Ramesh

    Ramesh Guest

    Hi,

    I need to convert unsigned long data to a std::string, I googled a bit
    to see if the string class supports any methods to achieve this - but
    didnt find any, I think of using sprintf and converting to a char buf
    and then putting it back in std::string -

    is anyone aware of any other way?

    Thanks
    /R
    Ramesh, Oct 10, 2008
    #1
    1. Advertising

  2. Ramesh

    Kai-Uwe Bux Guest

    Ramesh wrote:

    > Hi,
    >
    > I need to convert unsigned long data to a std::string, I googled a bit
    > to see if the string class supports any methods to achieve this - but
    > didnt find any, I think of using sprintf and converting to a char buf
    > and then putting it back in std::string -
    >
    > is anyone aware of any other way?


    If you are used to sprintf but want something that deals with std::string
    instead, you could try this:

    bool strprintf ( std::string & buffer, char const * format, ... ) {
    while ( true ) {
    buffer.resize( buffer.capacity() );
    int old_length = buffer.size();
    std::va_list aq;
    va_start( aq, format );
    int length_needed =
    vsnprintf( &buffer[0], old_length + 1, format, aq );
    va_end( aq );
    if ( length_needed < 0 ) {
    return ( false );
    }
    buffer.resize( length_needed );
    if ( length_needed <= old_length ) {
    return ( true );
    }
    }
    }


    WARNING: the above is not standard compliant; it assumes that std::string is
    contiguous (as far as I know, all implementations are, and it will be
    required by the next iteration of the standard C++0X); also it requires
    that is is legal to write a trailing 0 behind the string. Also, it assumes
    that vsnprintf() conforms to C99 or C++0X. Thus, you have to test this
    carefully.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 10, 2008
    #2
    1. Advertising

  3. Ramesh wrote:
    > is anyone aware of any other way?


    I really can't understand why two people have already answered your
    question with overly long and complicated answers, which are also
    basically just wrong. Come on, people, keep answers simple and
    concentrate on the *basics*.

    What you want is this:

    std::eek:stringstream os;
    os << yourUnsignedValue;
    std::string theValueAsString = os.str();
    Juha Nieminen, Oct 10, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    645
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,161
    Peter Shaggy Haywood
    Sep 20, 2005
  3. Replies:
    3
    Views:
    8,733
  4. Replies:
    127
    Views:
    3,538
    David Thompson
    Mar 10, 2008
  5. pozz
    Replies:
    12
    Views:
    700
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page