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

Started by
15 comments, last by vbisme 23 years, 5 months ago
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.
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
Advertisement
Why wouldn''t you use the itoa() function? Int-To-Array... Am I missing something here?

Regards,
Jumpster
Regards,JumpsterSemper Fi
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
Wout "Dormeur" NeirynckThe Delta Quadrant Development Page
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 - 0x30;<br><br> </i>
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 - 0x30;<br><br> </i>
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
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.

Volition, Inc.

This topic is closed to new replies.

Advertisement