1. ## Counting integer digits

I searched for "integer, length" in the forum search & didn't get any matches, so I assume this hasn't been answered:

How do I count the number of digits in an integer?

For instance, if the user enters the number one million (1000000) it returns a value of 7 (the number 1 + 6 zeros).

Do I need to convert it to character data for easier handling or can I do it with an integer?

Thanks!

mw

2. Code:
```int num;

if( num < 10 )
{
printf( "num is 1 digit" );
}
else if( num < 100 )
{
printf( "num is 2 digits" );
}

etc```
Of course what you do with negative numbers is up to you.

3. Is there a way to do it by actually counting the digits one at a time? With a dynamic-sized integer?

My next step will be to convert the number from hexidecimal to decimal, so I'll need to examine each digit individually.

Thanks!

mw

4. Code:
```int main( void )
{
int *buff;           // stores your data
int num = 0;      // stores number of digits
int ch;               // temp store for data

buff = malloc( sizeof( int ) );

while( ( ch = getchar() ) != EOF )
{
*buff = ch;
num++;
buff = realloc( buff, sizeof( int ) * num + 1 );
buff += num;   // move to unused space
}

free( buff );

return 0;
}```
something like this maybe( I think it should work!!!), although the digits have to entered individualy. Oh there is no error checking in the above code for brevity.

5. Or better do this
Code:
```int i;
int k, num = 1; //
//initialize  i;etc
if  (i<0)  i = -i;
while (i > 10)
{
k = i%10;// currently last digit
i = i/10; //like 1234 becomes 123 etc
num++;
}```

6. Originally Posted by Letto
Or better do this
Code:
```int i;
int k, num = 1; //
//initialize  i;etc
if  (i<0)  i = -i;
while (i > 10)
{
k = i%10;// currently last digit
i = i/10; //like 1234 becomes 123 etc
num++;
}```

Better still do this

Code:
`int res = (int) log10(number) + 1;`

Mezzano

7. Only that he needs to examine each digit individually

8. Originally Posted by Letto
Only that he needs to examine each digit individually
Yes but with the other ways suggested he must do it twice, he must loop once through the number just to calculate how large the array needs to hold the string representation of it. Then he needs to loop through that array to convert each digit. The way I suggested cuts out the first loop as all he really cares about, according to his original post, is the number of digits in the number. Of course his follow up post doesn't make much sense...he wants to know how to find the number of digits in an integer, fine and good, I gave him that. Then he says he needs to convert from "hex" to decimal. I don't get what he means by this, if he has the value in an integer it is already in decimal form (actually it is in binary and you can interpret that in whatever base you wish), if he has it in a string then he doesn't need to know the length. The whole question is non-sensical.

Mezzano

9. What I'm trying to do is take a hexidecimal integer of random size & convert it to the decimal equivalent. To do this, I wanted to set up a loop based on the size of the number & multiply each number by 16 to a certain power (depending on the location of the digit).

For instance, if I have the number 0063FDF0 in hex, I would start from the right digit & move to the left:

0 * 16 ^ 0 = 0
F(15) * 16 ^ 1 = 240
D(13) * 16 ^ 2 = 3328
F(15) * 16 ^ 3 = 61440
3 * 16 ^ 4 = 196608
6 * 16 ^ 5 = 6291456
0 * 16 ^ 6 = 0
0 * 16 ^ 7 = 0

Next I would add these numbers up:
0 + 240 + 3328 + 61440 + 196608 + 6291456 + 0 + 0 = 6553072

So 0063FDF0 in hexadecimal is equivalent to 6,553,027 in decimal.

This probably isn't the best way to do it, but it's the way I was taught.

mw

10. Originally Posted by Lionmane
What I'm trying to do is take a hexidecimal integer of random size & convert it to the decimal equivalent.
Do you mean the text representation of an integer value? Can you use [s]printf?
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
unsigned int hex = 0x63FDF0;
char *decimal = malloc(log10(hex) + 2);
if ( decimal )
{
sprintf(decimal, "%u", hex);
printf("hex = %X (%u), decimal = %s\n", hex, hex, decimal);
free(decimal);
}
return 0;
}

/* my output
hex = 63FDF0 (6553072), decimal = 6553072
*/```

11. Code:
```sprintf( buf, "%d", number );
length = strlen( buf );```

Quzah.

12. Originally Posted by quzah
Code:
```sprintf( buf, "%d", number );
length = strlen( buf );```

Quzah.
lol. Very nicely done, quzah

13. Code:
`int length = sprintf( buf, "%d", number );`

14. So I have to convert the number to text in order to count the digits?

mw

15. No. But it's the easiest most simplest way.