Thread: selecting an array

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    selecting an array

    I have an integer
    for ex.
    unsigned int a =120

    1)How can i parse the digits of the integers seperately.
    so as to have three integers.

    b=1
    c=2
    d=0

    ???

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    Thanks for the help salem but,

    could you be a little bit more specific? The problem is that i use Keil(for embedded systems) compiler and i cannot see the result so easily.

    Where can i find a c reference (so as to find info about sprintf?)

    Thanks once more

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    i can't understand

    I am trying to do this.


    sprintf(*velocity1, "%.1velocity", velocity);

    So as to get the first character of the integer velocity and set it as the integer but it doesn't work. I found something like this in the Ritchie book. Can you some give me an example?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Can you some give me an example?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       unsigned int i, digit, a = 120;
       char buffer[11];
    
       sprintf(buffer, "%u", a);
    
       printf("a = %u, buffer = \"%s\"\n", a, buffer);
       for(i = 0; i < 3; ++i)
       {
          digit = buffer[i] - '0';
          printf("buffer[%u] = '%c' (%d), digit = %u\n",
                 i, buffer[i], buffer[i], digit);
       }
       return(0);
    }
    
    /* my output
    a = 120, buffer = "120"
    buffer[0] = '1' (49), digit = 1
    buffer[1] = '2' (50), digit = 2
    buffer[2] = '0' (48), digit = 0
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    exidiot
    Guest
    Includes code to get each digit mathematically.
    Code:
    int DecToHex(int nDec) {
    
    	/* Converts a decimal number so that it looks the same when shown in 
    	 * hexadecimal. eg. 313 becomes 787 which is 0x313 in hexadecimal */
    
    	int nHex = 0, nHexBase = 1, nDecBase = 1, nDigitCount = 0, nNextDigitCount = 0;
    
    	do {
    		nDigitCount = nDec / nDecBase;
    		nNextDigitCount = nDec / (nDecBase * 10);
    		nDigitCount -= nNextDigitCount * 10;
    		nHex += nHexBase * nDigitCount;
    
    		nDecBase *= 10;
    		nHexBase *= 16;
    
    	} while (nNextDigitCount != 0);
    
    	return nHex;
    }

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    I have this code

    ------------------------------------------------------------
    unsigned int digit0, digit1, velocity = 314;

    char buffer[11];
    sprintf(buffer, "%u", velocity);

    digit0 = buffer[0] - '0';
    digit1 = buffer[1] - '0';
    //digit2 = buffer[2] - '0';

    switch(digit0){
    case(1): data_write1(0, 5, number1[0][0], number1[0][1], &number1[0][0]);
    case(3): data_write1(0, 5, number3[0][0], number3[0][1], &number3[0][0]);
    case(4): data_write1(0, 5, number4[0][0], number4[0][1], &number4[0][0]);
    }

    switch(digit1){
    case(1): data_write1(0, 36, number1[0][0], number1[0][1], &number1[0][0]);
    case(3): data_write1(0, 36, number3[0][0], number3[0][1], &number3[0][0]);
    case(4): data_write1(0, 36, number4[0][0], number4[0][1], &number4[0][0]);
    }
    -------------------------------------------------------------

    the datawrite write a number(1,3 or 4). The problem is that it is looping all the time. It is taking the three cases as right.

    What is the problem?
    Shouldn't the buffer[0] be the first digit of the int velocity?

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    sorry i am blind,

    it's ok. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM