Counting integer digits

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 05-22-2005
Lionmane
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
• 05-22-2005
paul_uk
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.
• 05-22-2005
Lionmane
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
• 05-22-2005
paul_uk
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     }     // add processing code here   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.
• 05-22-2005
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++; }```
• 05-22-2005
Mezzano
Quote:

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
• 05-22-2005
Letto
Only that he needs to examine each digit individually ;)
• 05-22-2005
Mezzano
Quote:

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
• 05-22-2005
Lionmane
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
• 05-22-2005
Dave_Sinkula
Quote:

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 */```
• 05-22-2005
quzah
Code:

```sprintf( buf, "%d", number ); length = strlen( buf );```
;)

Quzah.
• 05-22-2005
xeddiex
Quote:

Originally Posted by quzah
Code:

```sprintf( buf, "%d", number ); length = strlen( buf );```
;)

Quzah.

lol. Very nicely done, quzah :)
• 05-22-2005
Dave_Sinkula
Code:

`int length = sprintf( buf, "%d", number );`
:p
• 05-22-2005
Lionmane
So I have to convert the number to text in order to count the digits?

mw
• 05-22-2005
xeddiex
No. But it's the easiest most simplest way.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last