How do I get the first, second or third number directly, from an existing integer?For example, if x is considered to be 375635, is there any way to ask the board "Whats the first/second/third digit?" Thanks in advance.
What range of number values do you need to do this for ?
atoi (Ascii to integer) sticks the digits in an array and you can then access them one at a time:
char myChar[10];
int myInt = 12345;
void setup()
{
Serial.begin(9600);
Serial.println(itoa(myInt, myChar, 10));
Serial.println(myChar[0]);
Serial.println(myChar[1]);
Serial.println(myChar[2]);
Serial.println(myChar[3]);
Serial.println(myChar[4]);
} //setup
void loop()
{
} //loop
12345
1
2
3
4
5
Is it a 6-digit decimal number : 000000 to 999999?
Or ltoa, Long to Ascii:
char myChar[10];
long myLong = 12435798;
void setup()
{
Serial.begin(9600);
Serial.println(myLong);
ltoa(myLong, myChar, 10);
Serial.println(myChar[0]);
Serial.println(myChar[1]);
Serial.println(myChar[2]);
Serial.println(myChar[3]);
Serial.println(myChar[4]);
Serial.println(myChar[5]);
Serial.println(myChar[6]);
Serial.println(myChar[7]);
} //setup
void loop()
{
} //loop
12435798
1
2
4
3
5
7
9
8
The array needs to be long enough to hold the digits and the terminating null.
as an excercise you can do it yourself ,right ? divide by 10^n round to integer and store your digit...
Untested:
uint8_t getDigit(uint32_t num, uint8_t digit) {
uint32_t tempNum;
for (uint8_t i = 0; i < digit; i++) {
num /= 10;
}
tempNum = num / 10;
tempNum *= 10;
return num - tempNum;
}
Thanks a lot!!!! I finally got my displays working.