0

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
}
Community
  • 1
  • 1
Aramil
  • 167
  • 6

1 Answers1

1

To convert a byte[] to Int32 or to a float you can use the BitConverter class.

As stated in my comment you need to work out a ratio from the old range to the new range. Assuming rmin and rmax are your new range min and new range max values then you need to also supply the old min and max values. Something like this.

Community
  • 1
  • 1
sr28
  • 4,728
  • 5
  • 36
  • 67
  • Yes, but as far as I know this will give me "byte = 0xFF" >> "int = 255". I need a way to convert it into other ranges where "0xFF" gives me "1000", "555", etc. And I'm looking for automated solution so I don't have to make tables for every single range. – Aramil Mar 11 '16 at 12:17
  • @Aramil - in that case you need the old max and min values as well as the new min and max values. You can then create a ratio and convert from there. – sr28 Mar 11 '16 at 12:29