Archived

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

Macke

Can you crack this nut.......?????

Recommended Posts

Im making a numberlibrary...I send in a ínteger and it is displayed with my own number font. But I have a problem. I want to take out all numbers from an integer and put them in a vector. Example: Integer value: 1265 Vector value: v[0]= 1 v[1]= 2 v[2]= 6 v[3]= 5 It doesn''t matter if it is a integer vector or a char vector. Can anybody help me?????

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Use _itoa(), or a similiar integer to string conversion routine, and then parse the string manually.

char s_number[10]; //enough space for any 32-bit int
int number = 12345678, x;
vector numstring;

_itoa(number, s_number, 10);

for(x=0; x<10; x++)
{
if(s_number[x] == ('' '' // ''\0'')) break;
else numstring.push_back(s_number[x]);
}

You should check for spaces or null terminators because numbers dont have them. And depending on the implementation of _itoa() you may or may not have a null terminator in the string at the end of the number. This code should work with any compiler, except for the _itoa(), which is Microsoft''s implementation.

Cauldron the Ancient

Share this post


Link to post
Share on other sites
Yep, strategicalliance has the idea..

If x is the number ( eg 1234 ),
x%10 = least significant digit (4).
((x - (x%10))/10)%10 = second-least significant digit (3)
((x - ((x - (x%10))/10)%10 )/100)%10 = 2.
etc..


Give me one more medicated peaceful moment..
~ (V)^|) |<é!t|-| ~

Share this post


Link to post
Share on other sites