Return to Snippet

Revision: 43734
at March 30, 2011 01:47 by vikiyou


Initial Code
/**                                                                                                                                                          
  * C++ version 0.4 std::string style "itoa":                                                                                                                
  * Contributions from Stuart Lowe, Ray-Yuan Sheu,                                                                                                           
                                                                                                                                                             
  * Rodrigo de Salvo Braz, Luc Gallant, John Maloney and Brian Hunt                                                                                          
*/

#include <string>

std::string itoa_cpp(int value, int base)                                                                                                                
{                                                                                                                                                        
    std::string buf;                                                                                                                                     
																			  
    // check that the base if valid                                                                                                                      
    if (base < 2 || base > 16)                                                                                                                           
	return buf;                                                                                                                                      
																			  
    enum { kMaxDigits = 35 };                                                                                                                            
    buf.reserve( kMaxDigits );  // Pre-allocate enough space.                                                                                            
																			  
    int quotient = value;                                                                                                                                
																			  
    // Translating number to string with base:                                                                                                           
    do {                                                                                                                                                 
	buf += "0123456789abcdef"[ std::abs( quotient % base ) ];                                                                                        
	quotient /= base;                                                                                                                                
    } while ( quotient );                                                                                                                                
																			  
    // Append the negative sign                                                                                                                          
    if ( value < 0 )                                                                                                                                     
	buf += '-';                                                                                                                                      
																			  
    std::reverse( buf.begin(), buf.end() );                                                                                                              
    return buf;                                                                                                                                          
}

Initial URL
http://www.strudel.org.uk/itoa/

Initial Description
A implement of itoa with c++, std string.
itoa is function that convert a integer to a string.

Initial Title
itoa implement for std string

Initial Tags
c++

Initial Language
C++