C++ String: How to convert a numeric type to a string?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: C++ String: How to convert a numeric type to a string?

Hybrid View

  1. #1
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    C++ String: How to convert a numeric type to a string?

    Q: How to convert a numeric type to a string?

    A:

    The old C method (deprecated):

    Code:
    char c[10];   // simply large enough - don't forget the 
                  // extra byte needed for the trailing '\0' 
    int i = 1234;
    sprintf(c, "%d", i);
    See 'sprintf()' in e.g. MSDN for further details.


    Using 'CString':

    Code:
    int i = 1234;
    CString cs;
    cs.Format("%d", i);
    The format specifiers are the same as for 'sprintf()'. See the 'CString' documentation in MSDN - it is fairly straight forward.

    A word of warning: mismatching the format specifiers ('%d') and the actually passed parameters will lead to unpredictable results, both for 'sprintf()' and for 'CString::Format()'.


    The C++ way:

    Following sample shows a template function that uses Standard C++ classes to complete the task:

    Code:
    #include <string>
    #include <sstream>
    #include <iostream>
    
    template <class T>
    std::string to_string(T t, std::ios_base & (*f)(std::ios_base&))
    {
      std::ostringstream oss;
      oss << f << t;
      return oss.str();
    }
    
    int main()
    {
      // the second parameter of to_string() should be one of 
      // std::hex, std::dec or std::oct
      std::cout<<to_string<long>(123456, std::hex)<<std::endl;
      std::cout<<to_string<long>(123456, std::oct)<<std::endl;
      return 0;
    } 
    
    /* output:
    1e240
    361100
    */
    This method is not only very elegant, but also type safe, because the compiler will pick the proper 'std::ostringstream::operator <<()' at compile time, according to the operand type.


    Last edited by Andreas Masur; July 24th, 2005 at 12:03 PM.

  2. #2
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773
    There is a new method with 'boost::format', which combines the advantages of printf with the type-safety and extensibility of streams.

    One of the advantages (as with printf) is you can store the entire format string as a template (not a C++ template). This is better for internationalisation (making string tables), as well as the fact that even just using a local string table can significantly reduce the code-size.


    Last edited by Andreas Masur; July 24th, 2005 at 12:03 PM.

  3. #3
    Join Date
    Nov 2008
    Posts
    13

    Re: C++ String: How to convert a numeric type to a string?

    Another method is top use itoa() function that is there in many Windows compilers. I myself have tested in Dev-C++ and VC++

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center