I know better how to do it with strings than numbers, but I can see a mathematical way to do it with numbers.
So , say you want the values 1, 12, 123, 6, and 54. Your max place is hundreds (123), but we'll be dividing, so you actually need to use 1000 for place_increment (because 54/1000 = 0.054), and you'd pad any numbers that didn't go up to the hundreds place with zeros. Because of this, it's possible for leading zeros in front of the first number to be stripped, so either use floats (I don't know how far they can go), or put some number in the very front that will be ignored, like just 1.
Your number might look like: 1001012123006054.
So now, you have to get each of those numbers out.
last_value = D#_value - (floor(D#_value / place_increment)) * place_increment)
Here's what this will do. For the above example, your place increment is 1000.
The first thing you're doing (the innermost parenthesis) is grabbing the next lowest integer to D#_value / 1000, which just moves the 54 3 places: 1001012123006.054 and then removes the 54: 1001012123006.
Now, so your subtraction will work properly, you have to multiply the new answer by your place_increment again, and get: 100101212300600.
Finally, you subtract this answer from the original answer.
1001012123006054 - 100101212300600
And you get 054, which simply loses the extra zero and becomes 54.
You can then repeat this process in a loop as many times as you need to to get to the #th number. At the end of your loop, make sure to change d_val to floor(d_val/1000), because you're going to want to still be able to use 1000 as place_increment the next round.
In the end, the function should look something like this:
int maskint(int d_val, int end_place, int place_increment) {
int place = 1;
place_increment = Pow(10, place_increment);
int last_int = 0;
while(place < end_place) {
last_val = d_val - (Floor(d_val / place_increment) * place_increment);
d_val = Floor(d_val / place_increment);
place += 1;
}
return last_val;
}
The above code changes place_increment to just accept how many places to move each time, and turns that into the right number by using place_increment = 10^place_increment.
It could still use some error checking, for instance, if the end_place you return is longer than the number of places at place_increment that are available, your program will start returning 0, or maybe crash.
You could probably fix this by, after the code that turns place_increment into a power of 10, checking that floor(d_val / Pow(place_increment, end_place)) > 0. There's still not much you could do besides return 0 and exit; since you can't print warning/fail messages to the user or Allegro.log yet, as far as I know. But it'll at least prevent a crash, if one might occur.
Hope this works, and hope this code works!
I saw the concept for the math long before I got the exact equasion worked out, so I *really* hope it works like it should. I tested the math on a graphing calculator, step by step doing what the program would do and then putting it in with X as d_val into the graphing system and looking at the values on a table. It seemed to work, so I just hope it does. ^_^
EDIT: Alright...upon testing, I discovered that its truncation always goes four digits on either side.
1234.1234
That's as far as you can go. Sticking with double-digits, you could max 4 numbers out of a single mask this way. And with single you could get 7, or 8 if your first digit is greater than 0.
So that DOES offer a lot of single-digit values to pass, either 64 or 56, and a total of either 24 or 32 with double and then 24 with triple. So still not too bad.
EDIT #2:
Updated the code so it works properly with 1234.1234 format numbers. Also removed the while loop.
For some reason I had to separate the operations on last_val; when I didn't, it was doing funny things. Now it's working perfectly, and returns an integer (though with trailing decimal zeros).
int maskint(int d_val, int end_place, int place_increment) {
place_increment = Pow(10, (place_increment * end_place));
int last_val = d_val / place_increment;
last_val = last_val * place_increment;
last_val = d_val - last_val;
last_val = last_val * 100 * 100;
return last_val;
}
So it's basically the same function as above, except:
-place_increment is now set to the EXACT place of the value you're looking for, so when you divide, it'll truncate all the way the first time and not require a loop;
-the equasion that sets last_val has been split up over several operations, since the program did funny math when I left the equasion intact;
-and at the end, last_val is multiplied by 100 twice. This is so it doesn't truncate 10000, and 10000 is to get the number from a 0.xxxx to an xxxx.0000.
Also, this means it will work with *4* digit numbers too. Specifically, either 1 or 2 of them per D value, or 8 or 16 of them total.