I know is simple and I know that is asked many times but still I couldn’t find the thing I am looking for.
I have a value from 0 to 500 it is a int.
I want to have an output from 0x00 to 0xFF.
public void method(int i)// 0-500
{
return the_proportional_value_in_hex_in_range_0_to_ff;
}
can someone writhe me a converter from this kind. Thanks
EDIT
I DO NOT wanted a exact conversion, I just wanted a SCALED conversion, and @Mac answered exactly what I needed.
int scaled = (i * 255) / 500;i / 500scalesito the range0 ... 1, then multiply by 255 (i.e.0xFF) scales that to0 ... 255. Doing it the other way around (multiply then divide) is just to prevent numeric underflow on the divide.To print as hex, you could use
System.out.printf("%x", scaled);. I say print, because talking about a “hex value” isn’t terribly useful.