static byte[] byteArrayToBase128Utf8(byte[] data) { int n = l(data); byte[] out = new byte[idiv_ceil(n, 7)]; int iIn = 0, iOut = 0; while (iIn < n-7) { // take top 7 bits of byte 0 out[iOut++] = (byte) (ubyteToInt(data[iIn]) >> (8-7)); // take bottom bit of byte 0 + top 6 bits of byte 1 out[iOut++] = (byte) 0x7F & (ubyteToInt(data[iIn]) << (7-1) | ubyteToInt(data[iIn+1]) >> (8-6)); // take bottom 2 bits of byte 1 + top 5 bits of byte 2 out[iOut++] = (byte) 0x7F & (ubyteToInt(data[iIn+1]) << (7-2) | ubyteToInt(data[iIn+2]) >> (8-5)); // take bottom 3 bits of byte 2 + top 4 bits of byte 3 out[iOut++] = (byte) 0x7F & (ubyteToInt(data[iIn+2]) << (7-3) | ubyteToInt(data[iIn+3]) >> (8-4)); // take bottom 4 bits of byte 3 + top 3 bits of byte 4 out[iOut++] = (byte) 0x7F & (ubyteToInt(data[iIn+3]) << (7-4) | ubyteToInt(data[iIn+4]) >> (8-3)); // take bottom 5 bits of byte 4 + top 2 bits of byte 5 out[iOut++] = (byte) 0x7F & (ubyteToInt(data[iIn+4]) << (7-5) | ubyteToInt(data[iIn+5]) >> (8-2)); // take bottom 6 bits of byte 5 + top bit of byte 6 out[iOut++] = (byte) 0x7F & (ubyteToInt(data[iIn+5]) << (7-6) | ubyteToInt(data[iIn+6]) >> (8-1)); // take bottom 7 bits of byte 6 out[iOut++] = (byte) 0x7F & ubyteToInt(data[iIn+6]); iIn += 7; } // TODO: rest ret out; }