code: public static int encode(BigInteger value, byte[] dst, int dstOffset) {
          /* Encoding of first byte:
  
          0x00:       null low (unused)
          0x01:       negative signum; four bytes follow for value length
          0x02..0x7f: negative signum; value length 7e range, 1..126
          0x80..0xfd: positive signum; value length 7e range, 1..126
          0xfe:       positive signum; four bytes follow for value length
          0xff:       null high
          */
  
          if (value == null) {
              dst[dstOffset] = NULL_BYTE_HIGH;
              return 1;
          }
  
          byte[] bytes = value.toByteArray();
          // Always at least one.
          int bytesLength = bytes.length;
  
          int headerSize;
          if (bytesLength < 0x7f) {
              if (value.signum() < 0) {
                  dst[dstOffset] = (byte) (0x80 - bytesLength);
              } else {
                  dst[dstOffset] = (byte) (bytesLength + 0x7f);
              }
              headerSize = 1;
          } else {
              dst[dstOffset] = (byte) (value.signum() < 0 ? 1 : 0xfe);
              int encodedLen = value.signum() < 0 ? -bytesLength : bytesLength;
              DataEncoder.encode(encodedLen, dst, dstOffset + 1);
              headerSize = 5;
          }
  
          System.arraycopy(bytes, 0, dst, headerSize + dstOffset, bytesLength);
  
          return headerSize + bytesLength;
      }
docs: Encodes the given optional BigInteger into a variable amount of
  bytes. If the BigInteger is null, exactly 1 byte is written. Otherwise,
  the amount written can be determined by calling calculateEncodedLength.
  
  @param value BigInteger value to encode, may be null
  @param dst destination for encoded bytes
  @param dstOffset offset into destination array
  @return amount of bytes written
  @since 1.2