# Extracting individual digits from a short

• 08-29-2001
G'n'R
Extracting individual digits from a short
Hi,

I need to pass 2 shorts to a function and then extract each individal digit in the shorts to pass on to another function. E.g if i pass 90 as the value in the lower variable i first have to format it corretly (90 must be 0090 etc) and then pass each of digits to a new function to be displayed larger on screen etc. So

void displayNumber(short lower, short higher){
char lowerBuffer[50]={0};
char higherBuffer[50]={0};
char temp[10]={0};
short number;

/* using sprintf to format the number */
sprintf(lowerBuffer, "%04u", lower);
sprintf(higherBuffer, "%04u", higher);

/* get the digit i want and and the null term. */
temp[0] = lowerBuffer[0]; temp[1] = "\0";
number = atoi(temp)); /* convert it to number */
/* use function to display it on screen */
_OPTREX_number(number, 5, 10); /* ignore the two last par.*/

temp[0] = lowerBuffer[1]; temp[1] = "\0";
number = atoi(temp));
_OPTREX_number(number, 6, 10); /* ignore the two last par.*/

temp[0] = lowerBuffer[2]; temp[1] = "\0";
number = atoi(temp));
_OPTREX_number(number, 7, 10); /* ignore the two last par.*/

temp[0] = lowerBuffer[3]; temp[1] = "\0";
number = atoi(temp));
_OPTREX_number(number, 8, 10); /* ignore the two last par.*/

_OPTREX_number(10, 5, 10); /*10 is a colon */

temp[0] = higherBuffer[0]; temp[1] = "\0";
number = atoi(temp));
_OPTREX_number(number, 10, 10); /* ignore the two last par.*/

temp[0] = higherBuffer[1]; temp[1] = "\0";
number = atoi(temp));
_OPTREX_number(number, 11, 10); /* ignore the two last par.*/

temp[0] = higherBuffer[2]; temp[1] = "\0";
number = atoi(temp));
_OPTREX_number(number, 12, 10); /* ignore the two last par.*/

temp[0] = higherBuffer[3]; temp[1] = "\0";
number = atoi(temp));
_OPTREX_number(number, 13, 10); /* ignore the two last par.*/

}

So if i pass 900 and 300 on the display it would be: 0900:0300.

My question is:

Is there any quicker way in doing this. e.g. is the a way to convert use the lower and higher buffers more directly to convert it to a single digit. e.g _OPTREX_number(/*conversomehow)lowerBuffer[0], 5, 10)

G'n'R
• 08-29-2001
G'n'R
Hi,

I have found a way. I can do this:

char lowerBuffer[50] is: 0900.

_OPTREX_number((short)(lowerBuffer[0]-48, 5, 8); or

in a printf function:

printf("%u", (unsigned short)lowerBuffer[0]-48); /* output 0 */

Since the dec value of lowerBuffer[0] will always be 48 greater than the dec value i want.

BUT, is this safe??

G'n'R
• 08-29-2001
>>> BUT, is this safe??

Yes, the ASCII/ANSI codes for the digits 0-9 are 0x30 - 0x39, therefore subtracting 0x30, (decimal 48), from the ASCII/ANSI code of a digit will give you the numerical value of the digit, (and vice versa of course).

You might want to investigate using loops in your routine. It will reduce the amount of code substantially.
• 08-29-2001
G'n'R
cool thanks. I will check out the loop thing.

G'n'R
• 08-29-2001
zen
If you want to seperate the digits from a number, padding it to 4 digits and don't want to have to convert to a character and back again then something like this will do the trick -

Code:

```int main() {     short i,j,firstnumber = 44,secondnumber=36;     short firstdigits [4]={0},seconddigits[4]={0};         for (i=1000,j=0;i>0;i/=10,j++)     {         firstdigits[j] = firstnumber/i;         firstnumber%=i;     }                 for (i=1000,j=0;i>0;i/=10,j++)     {         seconddigits[j] = secondnumber/i;         secondnumber%=i;     }             return 0; }```
but if you want to maintain the original numbers you'll have to make a copy of them before the for loops.
• 08-30-2001
G'n'R
Nice solution Zen. I have saved it. Thanks

G'n'R
• 08-30-2001
Raghu
Hi
another easy way of doing it

int main(int argc, char* argv[])
{
char str[5];
short arr[4];
num=30;
sprintf(str,"%04u",num); /* str -> 0030 */
sscanf(str,"%01d%01d%01d%01d",&arr[0], &arr[1], &arr[2], &arr[3]); /* arr -> {0,0,3,0} */
}
• 08-30-2001
Raghu
sorry missed out the "int"
int main(int argc, char* argv[])
{
char str[5];
short arr[4];
short num=30;

sprintf(str,"%04u",num); /* str -> 0030 */
sscanf(str,"%01d%01d%01d%01d",
&arr[0],&arr[1],&arr[2],&arr[3]); /* arr -> {0,0,3,0} */
}
• 08-30-2001
Witch_King
Code:

```#include<stdio.h> int main() {         int i = 90;         printf("%.4d",i);         return 0; }```
But if you need the number for computations than extract the numbers from the original variable using '/' and '%'.
• 08-30-2001
Witch_King
Code:

```int main() {         int x = 90;         char buffer[10];         sprintf(buffer,"%.4d",x);         printf("%s",buffer);       return 0; }```
I think that you have to use a string if you want the zero's infront.