Below is a class that encodes unsigned primitive types as a byte array and returns an encoded byte array as a decimal string. I understand conceptually how encodeIntBigEndian
& byteArrayToDecimalString
work. However, I'd appreciate clarity on:
- Why/how shifting the
val
by((size - i - 1) * Byte.SIZE)
produces an unsigned java byte value.- Also, why does applying a byte mask of
0xff
convert the byte to a decimal string value.
public class BruteForceCoding {
private static byte byteVal = 101; // one hundred and one
private static short shortVal = 10001; // ten thousand and one
private static int intVal = 100000001; // one hundred million and one
private static long longVal = 1000000000001L;// one trillion and one
private final static int BSIZE = Byte.SIZE / Byte.SIZE;
private final static int SSIZE = Short.SIZE / Byte.SIZE;
private final static int ISIZE = Integer.SIZE / Byte.SIZE;
private final static int LSIZE = Long.SIZE / Byte.SIZE;
private final static int BYTEMASK = 0xFF; // 8 bits
public static String byteArrayToDecimalString(byte[] bArray) {
StringBuilder rtn = new StringBuilder();
for (byte b : bArray) {
rtn.append(b & BYTEMASK).append(" ");
}
return rtn.toString();
}
public static int encodeIntBigEndian(byte[] dst, long val, int offset, int size) {
for (int i = 0; i < size; i++) {
dst[offset++] = (byte) (val >> ((size - i - 1) * Byte.SIZE));
}
return offset;
}
public static void main(String[] args) {
byte[] message = new byte[BSIZE + SSIZE + ISIZE + LSIZE];
// Encode the fields in the target byte array
int offset = encodeIntBigEndian(message, byteVal, 0, BSIZE);
offset = encodeIntBigEndian(message, shortVal, offset, SSIZE);
offset = encodeIntBigEndian(message, intVal, offset, ISIZE);
encodeIntBigEndian(message, longVal, offset, LSIZE);
System.out.println("Encoded message: " + byteArrayToDecimalString(message));
}
}