#### Archived

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

# int 123 to array[3] = {1, 2, 3}

## Recommended Posts

let''s say:
  int myInt; int myArray[5]; myInt = 63251; 
how do I take each digit in myInt and place it in each element of myArray so that myArray[0] = 1(which is the unit), myArray[1] = 5(the ten) ...and so on... one method I could think of is test how big myInt is, if > 10000, myArray[5] = myInt / 10000; this gives 6 so myArray[5] would be 6, then int temp; temp = myInt - 10000 the repeat the dividing. This is cumbersome however, could anybody tell me a better way?

##### Share on other sites
Hi,

Have''nt really thought this out, but you can convert the int into a character string by :
int num = 1234;
char numStr[5];

itoa(num,numStr,10);

then you have each element in the character string :
numStr[1] - 10s,
numStr[2] - 100s,
numStr[3] - 1000s,
etc...

if you need integers, just convert back using atoi(...)

/ Tooon

##### Share on other sites
Sorry, for providing you width such a lousy solution above,
just slept for 2h this night

Better way :

int num = 61234;
int numArr[4];

numArr[4] = num/10000; // result = 6
numArr[3] = (num%10000)/1000; // result = 1
numArr[2] = (num%1000)/100; // result = 2
numArr[1] = (num%100) /10; // result = 3
numArr[0] = (num%10); // result = 4

/ Tooon

##### Share on other sites

i would do it something like this...

int myInt;
int myArray[5];
int i;

myInt = 63251;
for(i = 0; i < 5; i++)
{
myArray = (myInt / pow(10, i)) % 10;
}

not "super-effecient" but it does what it''s suppose to and i think its better than using atoi & itoa...

##### Share on other sites
Nice!

One more question, how do you test for how many digit any give number has?

##### Share on other sites
Hmm... when i needed a solution to that problem i used (rather ugly, a last resort):

if (num < 10) digits = 1;
else if (num < 100) digits = 2;
else if (num < 1000) digits = 3;

etc.

EDIT: i've just thought of a better one:

  #include #include #define MAX_NUM_OF_DIGITS 10char buffer[10];int digits, itemp, original_number;printf ("\nEnter a number: ");scanf("%d", &original_number);for (int i=1; i original_number) { digits = i; break; }}printf("\n%d\n", digits);

Edited by - jumble on November 2, 2000 5:23:03 AM

##### Share on other sites
digits= 0;
for( iTempnum= num; iTempnum != 0; iTempnum /= 10 ) numArr[ digits++ ]= iTempnum%10;

##### Share on other sites
Here''s my solution that can handle any number. Have you skipped your math lessons when you can''t use logarithm?

  void Int2Array(unsigned long value, unsigned char* array){ unsigned long digits; digits = log(value) + 1; array = new unsigned char[digits]; for(unsigned long index = 0 ; index < digits; index++) { array[index] = value % 10; value /= 10; }}

-Jussi

"Paina pääsi rauhaansa,
lepää hetki lennostasi,
anna maailman mennä tietään,
aina suurta yötä päin"

- CMX

##### Share on other sites
Hey Selkrank, I dare you to try that out with a negative number . And you should use log10 (log with base number 10). And remember: log10(n) = log(n)/log(10).

Why not use this:

  #include int n = 64253; // Or another numberint numDigitsInNumber = (int)floor(log10(abs(n)))+1;

Dormeur

Edited by - Dormeur on November 2, 2000 8:47:14 AM

##### Share on other sites
quote:
Original post by Dormeur

Hey Selkrank, I dare you to try that out with a negative number . And you should use log10 (log with base number 10). And remember: log10(n) = log(n)/log(10).

As you can see, I declared value an unsigned long, so it cannot be negative. And if I remember right, log() function is the 10-based logarithm, not e-based.

quote:
Why not use this:

  #include int n = 64253; // Or another numberint numDigitsInNumber = (int)floor(log10(abs(n)))+1;

What does floor() do?

-Jussi

"My spine hurts"

Edited by - Selkrank on November 2, 2000 8:51:35 AM

##### Share on other sites
actually:

log10(x) = ln(x)/ln(10)

i thought the standard log() function was base2? not sure.

===============================================
If there is a witness to my little life,
To my tiny throes and struggles,
He sees a fool;
And it is not fine for gods to menace fools.

##### Share on other sites
Why wouldn''t you use the itoa() function? Int-To-Array... Am I missing something here?

Regards,
Jumpster

##### Share on other sites
Selkrank, Mithrandir: Officially log is indeed 10 based (and yes, log(x) = ln(x)/ln(10), I stand corrected), but sometimes log is 2 based or even log == ln, especially in software. I guess a guy might get confused .

Selkrank: floor() just chops of the decimals from a double (e.g. floor(0.5) == 0.0). I guess you could rely on the compiler''s implicit type casting abilities instead.

Of course you can use itoa, but then you''ll get a char array, not an int array, and you would have to subtract ''0'' from each char to get its actual value. I think it''s faster to just do it yourself, because who knows what that itoa function does inside?

Dormeur

##### Share on other sites
Here. No expensive math routines needed:

int num=12345;
int digits[MAX_DIGITS];
char buf[MAX_DIGITS];
int i;

itoa(num, buf, 0); // or sprintf() if you like
for(i=0; i digits = (int)buf[i] - 0x30;

##### Share on other sites
That last one got messed up.

int num=12345;
int digits[MAX_DIGITS];
char buf[MAX_DIGITS];
int i;

itoa(num, buf, 0); // or sprintf() if you like
for(i=0; i(lessthan)strlen(buf); i++)
digits = (int)buf[i] - 0x30;

##### Share on other sites
is it me??
nahh it''s you
welll

int x = 13464, i = 0;
while(x > 0)
{
arr[i++] = x % 10;
x /= 10;
}

so now you don''t have to use STRING funcs
hope it helps good luck

http://qsoft.cjb.net

##### Share on other sites
Guys, you''re making this _way_ too complicated.

int whee = 12345;
char str[32];

sprintf(str, "%d", whee);
_strrev(str);

If your compiler doesn''t have strrev(), write your own - its a trivial function.

• ### Forum Statistics

• Total Topics
628388
• Total Posts
2982403

• 10
• 9
• 16
• 24
• 11