What is the best way to compress a 16bit number into an 8bit number I looking to build height maps which are used in computer cartography. But the tech only gives me 8bits to store my height. Obviously there are some areas where the elevation will change far too much to fit in just 256 observations so I need to compress it.
I solved it by using percentages, eg.
starting height $521$m, ending height $4351$m. To squeeze my $16$bit values into $8$ I do this.
$\frac {521\text m}{4351\text m} = 0.13 * 255 = 30$
$\frac {4351\text m}{4351\text m} = 1 * 255 = 255$
Which works...but...I wonder if there is a better way, and indeed how can describe the compression factor using this method as a single number I can apply to the figures to get a true representation when I draw the map.
which is why I post the question here to ask you good people?
 A: So you need a lossy compression, and one wants a compression that conveys as much information as possible.  This in turn means that you have to know the distribution of the occuring heights.
For example, if values 0...254 are very common, but values of 255 or more are extremely rare or do not occur at all, then you can encode 0...254 as a height and 255 to mean "anything that's 255 or more".
A common way to encode values over a large range is floating-point like IEEE 754, in your case it could be:

*

*1 bit for the sign

*4 bits for the (biased) exponent

*3 bits for the mantissa (without leading 0)

and with an implicit scaling so that the values cover your 16-bit range.  Floating-point however is only useful if the values extend over several orders of magnitude, and you need a fine-grained encoding for numbers that are small in magnitude, and just coarse encoding if the values are big.  This basically means to try to minimize the relative error.
If the values are evenly distributed in 0...65535, then just divide by 256, with an optional rounding step.
A: Compared to what you described, maybe the formula
$$
x \mapsto \lfloor\frac{256(x-521)}{4352 - 521}\rfloor
$$
works a bit better.
