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
-
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
Any reason why you didn't use
itoa()
orsprintf()
? -
Any reason why you didn't use
itoa()
orsprintf()
?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.
-
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.
Look them up on Google or something. As a matter of fact, if you put "integer to string c++" in google you'll get a bunch of suggestions as to how to do it. Guess point is, don't reinvent the wheel if you don't have to. :)
-
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
Karsten Malmquist wrote:
I have been asking about three questions on this website about how to convert an integer number to a string
I've just had a quick look at your other questions and none of them mention that in their title. Also you are writing C code and yet not using the standard library methods (
sprintf()
for example). The code you have above could be simplified by a simple loop extracting the remainder of successively divided values. When 'converting' a digit to a character you use the expressiondigit + 48
which, while it works, does not make it obvious to readers of your code what you are doing; a better expression would bedigit + '0'
.The best things in life are not things.
-
Look them up on Google or something. As a matter of fact, if you put "integer to string c++" in google you'll get a bunch of suggestions as to how to do it. Guess point is, don't reinvent the wheel if you don't have to. :)
-
Albert Holguin wrote:
don't reinvent the wheel if you don't have to.
And if at all you have to, make sure it is a better wheel.
Excellent point :)
-
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
Is there any particular reason why you're not using
stringstream
? That's usually the C++ preferred way of converting integers/strings, withitoa
andsprintf
being preferred for C. -
Is there any particular reason why you're not using
stringstream
? That's usually the C++ preferred way of converting integers/strings, withitoa
andsprintf
being preferred for C.because the poster mentions C++ in the title?
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun -
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
you could just use itoa() or sprintf() as has been mentioned. But here's a better version of your function:
std::string get_value(int value)
{
std::string result = "";
if (value < 0)
{
value *= -1;
result = "-";
}do { int digit = value % 10; value /= 10; result = (char)(digit + (int)'0') + result; } while (value > 0) result = result.reverse(); return result;
}
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun