Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows API
  4. A function that converts an integer number to a string (C++)

A function that converts an integer number to a string (C++)

Scheduled Pinned Locked Moved Windows API
c++jsonhelptutorialquestion
10 Posts 5 Posters 16 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Karsten Malmquist
    wrote on last edited by
    #1

    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

    A L D T 4 Replies Last reply
    0
    • K Karsten Malmquist

      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

      A Offline
      A Offline
      Albert Holguin
      wrote on last edited by
      #2

      Any reason why you didn't use itoa() or sprintf()?

      K 1 Reply Last reply
      0
      • A Albert Holguin

        Any reason why you didn't use itoa() or sprintf()?

        K Offline
        K Offline
        Karsten Malmquist
        wrote on last edited by
        #3

        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.

        A 1 Reply Last reply
        0
        • K Karsten Malmquist

          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.

          A Offline
          A Offline
          Albert Holguin
          wrote on last edited by
          #4

          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. :)

          L 1 Reply Last reply
          0
          • K Karsten Malmquist

            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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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 expression digit + 48 which, while it works, does not make it obvious to readers of your code what you are doing; a better expression would be digit + '0'.

            The best things in life are not things.

            1 Reply Last reply
            0
            • A Albert Holguin

              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. :)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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.

              A 1 Reply Last reply
              0
              • L Lost User

                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.

                A Offline
                A Offline
                Albert Holguin
                wrote on last edited by
                #7

                Excellent point :)

                1 Reply Last reply
                0
                • K Karsten Malmquist

                  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

                  D Offline
                  D Offline
                  David Magnotti
                  wrote on last edited by
                  #8

                  Is there any particular reason why you're not using stringstream? That's usually the C++ preferred way of converting integers/strings, with itoa and sprintf being preferred for C.

                  T 1 Reply Last reply
                  0
                  • D David Magnotti

                    Is there any particular reason why you're not using stringstream? That's usually the C++ preferred way of converting integers/strings, with itoa and sprintf being preferred for C.

                    T Offline
                    T Offline
                    TheGreatAndPowerfulOz
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    • K Karsten Malmquist

                      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

                      T Offline
                      T Offline
                      TheGreatAndPowerfulOz
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups