java - How to store many small integers into a byte array? -


im making online game, , in many online games need loads of data being transfered via internet, need able compress data efficiently.

for instance want send client server character coordinates.

edit: yeah bad example, let me change values...

x coordinate(say -32 32).(65 diferent possible values) y coordinate(-32 32).(65 diferent possible values) z coordinate(-16 16).(33 diferent possible values)

i know x stored before y stored before z on byte array before sending.

i know in server x cannot lower -31 nor higher 32, same other values.

65*65*33 = 139.425 diferent possible combinations of values 3 numbers = 17 bits.

7 + 7 + 5 = 19 bits.

so if store x in first 7 bits, y in next 7 bits , z in next 5 bits take 19 bits , able read them in other side ease, since posible combinations of values 3 numbers can take take 17 bits store feel im losing space here. there way compress 3 numbers using less 19 bits?

of course 19 bits , 17 bits both need 3 bytes, if 17 bits , 15 bits make huge diference.

i believe after coding algorithm, not compression algorithm. compress numbers should know information these numbers.

for coding algorithm: have 65*65*33=139.425 different possible values. log2(139.425) ~ 17.09 therefore need @ least 18 bits code of these possible values. simple coding scheme said:

value = z*65*65 + y*65 + x 

then decode it:

x = value % 65 y = (value/65) % 65 z = (value/65/65) % 33 

now value integer. if want store in byte array split integer 3 bytes:

byte1 = value & 255; byte2 = (value>>8) & 255; byte3 = (value>>16) & 255; 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -