Q:

Java: custom to byte conversions

I am working with some low capacity module and I need to compress the data as much as possible. The data will look like this:
DeviceEvent:

1 byte:

2 bits for status (00 each time)
6 bits for rgb color (3 x 2 bits)

2 bytes: number of minutes from now to a certain datetime

I need to create a constructor (preferably 2 constructors) for conversion from/to:
Event:

byte[] color (rgb, colors will get simplified to only 64 available)
some datetime (but I will get the integer for difference in minutes and it will be small enough to fit in two bits)

So basically I need:

byte[3] color <-> 1 byte status and color  
int minutes <-> byte[2]
minutes

I will be thankful for any help

A:

I'm not very sure what is your problem, probably this will help:
final byte red = 1; // 01 binary
final byte green = 2; // 10 binary
final byte blue = 3; // 11 binary

final byte finalColor = (byte) ((red & 0x3) << 4) | ((green & 0x3) << 2) | (blue & 0x3);
System.out.println(finalColor);// finalColor is 011011 = 27 decimal

final int minutes = 0x1234; // first byte is 0x12, second byte is 0x34
final byte[] bytes = {(byte) (((minutes) >>> 8) & 0xff), (byte) (minutes & 0xff)};

System.out.println(bytes[0]); // 0x12 = 18 decimal
System.out.println(bytes[1]); // 0x34 = 52 decimal

