I have a question about translating an int value (say 15) into two separate values (1 and 5). This might be called something, but I am too new to know what to search for. The reason for this is to pass these values to two 74HC595 shift registers in order to display a score on a dual 7-segment display (so that I can display greater than 9). I have all the wiring set up, and the code for the game, I just need to know how to split the numbers and store them. Thanks in advance.
Something like this should do it
int val=15; char tens = val / 10 + '0'; char units = val - (tens * 10) + '0';
While I am storing the new values as ints in order to pass to the shift register functions, it worked anyways. I had actually thought about dividing by 10 for the tens value, but I didn't realize the decimal (1.5) would be parsed. Much appreciated though.
didn't realize the decimal (1.5) would be parsed
I'm not sure what you mean there, but when dealing with integer variables, any fractional part is discarded after a division operation. This is sometimes called "truncating division".
char tens = val / 10 + '0'; char units = val - (tens * 10) + '0';
tens is good, but you can have the units "faster" with:
char units = (val % 10) + '0';
(well, you'll save a few bytes on your board...)
tens is good, but you can have the units "faster" with...
I was feeling too lazy to explain the modulo function, and doing it the long way makes it more obvious what you're doing.
Thanks for all the replies/discussions. I got the setup working perfectly following this advice. Fruits of your help: