I can't figure out how to break a multi-digit number into single digit ones. I have googled it, and other people have had the same question, but the answers include functions and symbols that I am not familiar with. If someone could break it down into baby language for me, I would be most appreciative.
// initialize all digits to the empty pattern (no segments lit)
for (i=0;i<NUM_DIGITS;i++) digit_array*=0;*
// extract each digit (right to left)*
for (i=NUM_DIGITS-1;i>=0;i--) {*
// extract least significant digit as an index into the*
// segment pattern table* digit_array*=seg_def[number%10];
Serial.println(seg_def[number%10]);
_ // get next digit*_ * number=number/10;* * // exit if there are no more digits to avoid leading zeroes* * if (number==0) break;* _ }/for/
}/NumberToArray/_ What is a char? Why not use int i instead of char i? How do you use bytes? I understand what they are, but how would you call them to light certain segments of a 7-segment led? What is the %? I am trying to break down a multi-digit integer and seperate it into single-digit integers. I understand there are multiple ways to do this, but I don't understand the functions used to do it. Is there an index of commands that I can use to look up the correct usage of these functions I don't understand? Anyway, any help would be stellar.
There was another post that said the sprintf function wasn't a good one to use because it could cause buffer overflow errors. And I believe I read that strings in general are more burdensome than integers, etc. Also, the problem with atoi is, if I understand this C string handling - Wikipedia correctly, is it can't distinguish between a true zero and a blank character. The code that I submitted is supposed to not turn a multi-digit number into a string at all.
Have you forgotten how base-10 works? I am beginning to wonder if we need a tutorial (facepalm) on how base-10 works, because this question comes up on a regular basis, it seems...
Lets say you have a number: int num = 2957;
You want the '2', '9', '5', and '7' separate...
Divide it by 1000: int num1 = num / 1000;
Since num and num1 are integer variables, num1 will be "2" - there's your first number.
Multiply that by 1000: int temp = num1 * 1000;
Subtract the result (2000) from your original number: num = num - temp;
The variable num is now "957".
I'll leave it to you as to how to get the rest, but realize you have a "hundred's place", a "ten's place", and a "one's place"...
Perfect. I appreciate the response. I thought I remembered integers being rounded. I didn't see any threads that particularly made sense to me, but my discovery of the reference tab has helped me. I think there should be more tutorials outlining basic C++ principles. Perhaps one day, when I am more experienced, I'll make some.