What's a nice, readable way of getting the byte representation (i.e. a byte[]
) of an int
, but only using 3 bytes (instead of 4)? I'm using Hadoop/Hbase and their Bytes
utility class has a toBytes
function but that will always use 4 bytes.
Ideally, I'd also like a nice, readable way of encoding to as few bytes as possible, i.e. if the number fits in one byte then only use one.
Please note that I'm storing this in a byte[]
, so I know the length of the array and thus variable length encoding is not necessary. This is about finding an elegant way to do the cast.