code: private static void encodeBinary(byte[] bytes,
                                     int startpos,
                                     int count,
                                     int startmode,
                                     StringBuilder sb) {
      if (count == 1 && startmode == TEXT_COMPACTION) {
        sb.append((char) SHIFT_TO_BYTE);
      } else {
        if ((count % 6) == 0) {
          sb.append((char) LATCH_TO_BYTE);
        } else {
          sb.append((char) LATCH_TO_BYTE_PADDED);
        }
      }
  
      int idx = startpos;
      // Encode sixpacks
      if (count >= 6) {
        char[] chars = new char[5];
        while ((startpos + count - idx) >= 6) {
          long t = 0;
          for (int i = 0; i < 6; i++) {
            t <<= 8;
            t += bytes[idx + i] & 0xff;
          }
          for (int i = 0; i < 5; i++) {
            chars[i] = (char) (t % 900);
            t /= 900;
          }
          for (int i = chars.length - 1; i >= 0; i--) {
            sb.append(chars[i]);
          }
          idx += 6;
        }
      }
      //Encode rest (remaining n<5 bytes if any)
      for (int i = idx; i < startpos + count; i++) {
        int ch = bytes[i] & 0xff;
        sb.append((char) ch);
      }
    }
docs: Encode parts of the message using Byte Compaction as described in ISO/IEC 15438:2001(E),
  chapter 4.4.3. The Unicode characters will be converted to binary using the cp437
  codepage.
  
  @param bytes     the message converted to a byte array
  @param startpos  the start position within the message
  @param count     the number of bytes to encode
  @param startmode the mode from which this method starts
  @param sb        receives the encoded codewords