Well, as I said, I didn't get the answer i wanted, i've never heard of those functions to be honest, but thanks for the suggestion.
Karsten Malmquist
Posts
-
A function that converts an integer number to a string (C++) -
A function that converts an integer number to a string (C++)This function is written in C++. I have been asking about three questions on this website about how to convert an integer number to a string, but I never really got the answer I needed. The reason I asked those questions was because I was(/am) using the SDL API as a framework to help me program my games, now this API doesn't have any kind of input or output like DOS programs does (cin/cout). After asking these questions I decided to just write the function I needed myself, and I did. So for those who want to ask the same question as I did, then this could be your answer. This function takes a integer value that can be 32 bits at the most, then it converts it into a string and then it returns the string: std::string get_value( int value ) { //The string that will be the return value std::string ReturnValue = ""; //A copy of 'value' int valueCopy = value; //If the value is zero if ( value == 0 ) ReturnValue = (char)value + 48; //If the value is a negative value if ( value < 0 ) { ReturnValue = "-"; value = value - value - value; valueCopy = value; } int number[ 10 ]; //Convert the number to individual character values number[ 0 ] = valueCopy / 1000000000; valueCopy -= ( valueCopy / 1000000000 ) * 1000000000; number[ 1 ] = valueCopy / 100000000; valueCopy -= ( valueCopy / 100000000 ) * 100000000; number[ 2 ] = valueCopy / 10000000; valueCopy -= ( valueCopy / 10000000 ) * 10000000; number[ 3 ] = valueCopy / 1000000; valueCopy -= ( valueCopy / 1000000 ) * 1000000; number[ 4 ] = valueCopy / 100000; valueCopy -= ( valueCopy / 100000 ) * 100000; number[ 5 ] = valueCopy / 10000; valueCopy -= ( valueCopy / 10000 ) * 10000; number[ 6 ] = valueCopy / 1000; valueCopy -= ( valueCopy / 1000 ) * 1000; number[ 7 ] = valueCopy / 100; valueCopy -= ( valueCopy / 100 ) * 100; number[ 8 ] = valueCopy / 10; valueCopy -= ( valueCopy / 10 ) * 10; number[ 9 ] = valueCopy / 1; //Helps with dealing with the overflow of zero's bool SkipCharacter = true; //Add the converted character values to the the string that will be the return value for ( int n = 0; n < 10; ) { if ( SkipCharacter == false ) ReturnValue += (char)number[ n ] + 48; else if ( number[ n ] != 0 ) { SkipCharacter = false; ReturnValue += (char)number[ n ] + 48; } n++; } //Return the converted string return ReturnValue; } First of all, you can copy it and use it in your ap