Java BigInteger Calculate encodeBigIntNoTypeCode(BigInteger value)

Here you can find the source of encodeBigIntNoTypeCode(BigInteger value)

Description

encode Big Int No Type Code

License

Open Source License

Declaration

static byte[] encodeBigIntNoTypeCode(BigInteger value) 

Method Source Code

//package com.java2s;
/**//from  w  ww  . ja v  a 2 s  .  com
 * Copyright (C) 2009-2013 FoundationDB, LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.math.BigInteger;

public class Main {
    static byte[] encodeBigIntNoTypeCode(BigInteger value) {
        byte[] bytes = value.toByteArray();
        return floatingPointCoding(bytes, true);
    }

    /**
     * For encoding: if the sign bit is 1, flips all bits in the {@code byte[]};
     * else, just flips the sign bit.
     * <br><br>
     * For decoding: if the sign bit is 1, flips all bits in the {@code byte[]};
     * else, just flips the sign bit.
     * 
     * @param bytes - a Big-Endian IEEE binary representation of float, double, or BigInteger
     * @param encode - if true, encodes; if false, decodes
     * @return the encoded {@code byte[]}
     */
    static byte[] floatingPointCoding(byte[] bytes, boolean encode) {
        if (encode && (bytes[0] & (byte) 0x80) != (byte) 0x00) {
            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = (byte) (bytes[i] ^ 0xff);
            }
        } else if (!encode && (bytes[0] & (byte) 0x80) != (byte) 0x80) {
            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = (byte) (bytes[i] ^ 0xff);
            }
        } else {
            bytes[0] = (byte) (0x80 ^ bytes[0]);
        }

        return bytes;
    }
}

Related

  1. dumpNumber(PrintStream out, String s, BigInteger bi)
  2. emit(BigInteger integer)
  3. encode(BigInteger number, String alphabets)
  4. encode_long(BigInteger big)
  5. encodeBigInt(OutputStream out, BigInteger x)
  6. encodeCompactBits(BigInteger value)
  7. encodeFloat(BigInteger j, int e)
  8. encodeInteger(BigInteger i, OutputStream os)
  9. encodeMPI(BigInteger value, boolean includeLength)