Thread: Extracting individual digits from a short

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    55

    Question 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

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    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

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> 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.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    cool thanks. I will check out the loop thing.

    G'n'R

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    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.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    Nice solution Zen. I have saved it. Thanks

    G'n'R

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    2

    Lightbulb

    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} */
    }

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    2
    sorry missed out the "int"
    Read as
    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} */
    }

  9. #9
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    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 '%'.
    I compile code with:
    Visual Studio.NET beta2

  10. #10
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    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.
    I compile code with:
    Visual Studio.NET beta2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03:55 AM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. Say what? - Weird error.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2006, 11:54 PM
  4. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM
  5. My Allegro Program will not run...
    By Vicious in forum Game Programming
    Replies: 17
    Last Post: 06-14-2002, 12:49 AM