code: public static byte[] convertToExtendedUTF8(long value) {
      //calculate bytes needed
      int bytesNeeded = 1;
      for(int i = 0; i < 7; i++) {
        if(value >= limits[i] ) {
          bytesNeeded++;
        }
      }
      //create space
      byte [] result = new byte[bytesNeeded];
      int byteIndex = 0;
      int inputIndex = 0;
      int bytesLeft = bytesNeeded;
      while(bytesLeft > 1) {
        int midByteMarker = 0x80;//10 in leftmost bits
        int midByteMask = 0x3F;//00111111
        int val = ((int)(value >>> inputIndex) & midByteMask) | midByteMarker;
        result[byteIndex++] = (byte)val;
        inputIndex += 6;
        bytesLeft--;
      }
      int onesNeeded = inputIndex/6;
      if(onesNeeded > 0)
        onesNeeded++;
      int startMask = 255 >>> (onesNeeded + 1);
      int ones = 255 << (8-onesNeeded);
      int val = ((int)(value >>> inputIndex) & startMask) | ones;
      result[byteIndex++] = (byte)val;
  
      byte[] finalResult = new byte[bytesNeeded];
      for(int i = 0; i < bytesNeeded; i++) {
        int sourceIndex = bytesNeeded-1-i;
        int destIndex = i;
        finalResult[destIndex] = result[sourceIndex];
      }
      if(DEBUG_LEV > 10) {
        System.err.print("input:result_length:result :: " +value+":"+finalResult.length+"::");
        for(int i = 0; i < finalResult.length; i++)
          System.err.print(Integer.toHexString(finalResult[i])+":");
        System.err.println();
      }
      return finalResult;
    }
docs: Convert a value to an extended UTF8 format(as used in FLAC).
  @param value value to convert to extended UTF8(value must be positive
  and 36 bits or less in size)
  @return extended UTF8 encoded value(array size is equal to the number of
  usable bytes)