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
???
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
???
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
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?
>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.*
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; }
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?
sorry i am blind,
it's ok. Thanks!