I have a keypad that I use to enter digits into a microcontroller (atmega32 avr). The user will be able to enter 1, 2 or 3 digits, because I defined the input range will be [0-999]. Each of those digits will be stored in a position of an array of integers.
So, if my array is defined as int d[3]
and the user enters, in sequence, 2, 6, 9, I'll have d[0] == 2
, d[1] == 6
and d[2] == 9
. Now I want to make an integer from those digits {2, 6, 9}
: value == 269
. How can I do that?
The following answer should be applicable for any language. This would be fairly simple, I would advise you to format the values in your array a little differently. I think it would be easier if the operator enters a single-digit number store it like so: {0, 0, 5}
. If it was a double-digit number store it like so: {0, 5, 9}
. Finally, if the operator enters a three-digit number store it like so: {5, 9, 2}
.
Finally after you format the array you can use the following logic to get the number. For example if the array is arr = {5, 9, 8};
then you could do the following to get it as a number:
arr[0] * 100 + arr[1] * 10 + arr[2]
That would work if you follow the formatting advice I gave above.