breaking apart an int

Started by
8 comments, last by nextgengamer 21 years, 9 months ago
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
Advertisement
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
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
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]
-------------Ban KalvinB !
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]
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
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.
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.

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
quote:Original post by a person
snprintf(outBuff, OUT_BUFF_SIZE, "%s %08d", playerName, playerScore);


How about


  #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();  


Yes, I admit it, I''m a Boost addict.
(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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement