Public Group

#### Archived

This topic is now archived and is closed to further replies.

# breaking apart an int

This topic is 5988 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi does anyone know of a way to take an int and break it down into its seperate numbers. ex) int x = 1234; I want to do something like int y[4]; y[0] = 1; y[1] = 2; y[2] = 3; y[3] = 4; any help would be appreciated. thanks NextGenGamer

##### Share on other sites
I guess it wouldnt be the fastest way... but you could convert it to a string, separate it, then convert it back to int?

Simon

##### Share on other sites
Thats an idea im not too concerned with time at the time of this operation anyway since it is simply displaying the top 10 scores on screen

thanks for the idea though

Nextgengamer

##### Share on other sites
char szText[10];
int iScore = 1230;

itoa(iScore, szText, 10);

szText will now contain the number as a string.

You can access it like your example szText[0], szText[1], etc..

[edited by - granat on June 27, 2002 1:17:49 AM]

##### Share on other sites
For C, look into
sprintf

For C++, look into
< sstream > (NOT < strstream > !)

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on June 27, 2002 1:22:19 AM]

##### Share on other sites
You could also do it with some simple math if you want.

int num=1234;
int y[4];
int x=0;
int holder=1;
while(num!=0)
{
if(num%(holder*10)==0)
{
x++;
holder=holder*10;
}
num-=holder;
y[x]++;
}

should work.

##### Share on other sites
quote:
Original post by nextgengamer
Hi does anyone know of a way to take an int and break it down into its seperate numbers.

ex) int x = 1234;

I want to do something like

int y[4];
y[0] = 1;
y[1] = 2;
y[2] = 3;
y[3] = 4;

At first I wanted to say use left shifts, 4 bits for each digit, but that would only work in Hex. Consider how numbers are built

1234 = 1 * 1000 + 2 * 100 + 3 * 10 + 4 * 1

and work backwards from this - but if you''re seeking to display these number on screen, it''s probably best to use a string anyways.

##### Share on other sites
quote:
Original post by granat
itoa(iScore, szText, 10);

itoa is evil, non-standard, unportable and non typesafe.

quote:
Original post by Chem0sh
For C++, look into <sstream> (NOT <strstream> !)

Or go there, grab Boost''s lexical_cast.hpp which does it for you.

#include <boost/lexical_cast.hpp>#include <string>using namespace std;using namespace boost;string s = lexical_cast<string>(1234); // cast to stringstring s = lexical_cast<string>(123.4); // cast to stringlong i = lexical_cast<long>("1234");     // cast to longfloat f = lexical_cast<float>("123.4")  // cast to float

And so on... You''ll admit it is a bit more intuitive.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

##### Share on other sites
umm, why not the simple portable way?

  snprintf(outBuff, OUT_BUFF_SIZE, "%s %08d", playerName, playerScore);

much easier since you can format everything nice like for pronting staright to the screen instead of having to offset stuff. ie names and scores will take only a single line and be printed correctly. this can be used for quake style console support if you add \n to your printed buffer and your text output system handles that sort of thing.

##### Share on other sites
quote:
Original post by a person
snprintf(outBuff, OUT_BUFF_SIZE, "%s %08d", playerName, playerScore);

  #include <string>#include <sstream>#include <boost/format.hpp>using namespace std;using namespace boost;ostringstream ss;format frm( "%s %08d" );ss << frm % playerName % playerScore;string str = ss.str();

(Note: boost::format is still in beta)

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 16
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633360
• Total Posts
3011524
• ### Who's Online (See full list)

There are no registered users currently online

×