I'm trying to proportionally translate between two ranges of numbers. There is a byte from one side, and on the second side I have ints or floats with different ranges, including both ranges "0...X" and "-X...X".
How could I do it? Is there some automated way? I don't need big precision in rounding but I'd like to avoid making new table for every new range ;-) I've found one topic but it's about Python and it'll need re-basing every range not starting with zero: Translate numbers from a range to another range
Example Int template:
namespace MyProgram
...
void Program()
{
byte[] foo; // (0x00 ... 0xFF)
int bar; // int or float
int rmin = -500;
int rmax = 500;
foo = /*input*/;
bar = Do_Translation (foo, rmin, rmax);
...}
private int Do_Translation (byte[] foo, int rmin, int rmax)
{
if (foo[0] > 0x80)
{
//translate into range of 1...500
}
else if (foo[0] == 0x80)
{
return 0;
}
else if (foo[0] < 0x80)
{
//translate into range of -500...-1
}
}
Example float template:
namespace MyProgram
...
void Program()
{
byte[] foo; // (0x00 ... 0xFF)
float bar; // int or float
int rmin = 15.5;
int rmax = 100;
foo = /*input*/;
bar = Do_Translation (foo, rmin, rmax);
...}
private float Do_Translation (byte[] foo, int rmin, int rmax)
{
//translate byte range of 0x00...0xFF into float range of 15.5...100
}