public static int encodeDesc ( BigInteger value , byte [ ] dst , int dstOffset ) { /* Encoding of first byte: 0x00: null high (unused) 0x01: positive signum; four bytes follow for value length 0x02..0x7f: positive signum; value length 7e range, 1..126 0x80..0xfd: negative signum; value length 7e range, 1..126 0xfe: negative signum; four bytes follow for value length 0xff: null low */ if ( value == null ) { dst [ dstOffset ] = NULL_BYTE_LOW ; 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 ) ( bytesLength + 0x7f ) ; } else { dst [ dstOffset ] = ( byte ) ( 0x80 - bytesLength ) ; } headerSize = 1 ; } else { dst [ dstOffset ] = ( byte ) ( value . signum ( ) < 0 ? 0xfe : 1 ) ; int encodedLen = value . signum ( ) < 0 ? bytesLength : - bytesLength ; DataEncoder . encode ( encodedLen , dst , dstOffset + 1 ) ; headerSize = 5 ; } dstOffset += headerSize ; for ( int i = 0 ; i < bytesLength ; i ++ ) { dst [ dstOffset + i ] = ( byte ) ~ bytes [ i ] ; } return headerSize + bytesLength ; }
Encodes the given optional BigInteger into a variable amount of bytes for descending order . If the BigInteger is null exactly 1 byte is written . Otherwise the amount written can be determined by calling calculateEncodedLength .