Java Code Examples for java.math.BigInteger#byteValue()

The following examples show how to use java.math.BigInteger#byteValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Ipv6Block.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
private static Inet6Address longToAddress(final BigInteger l) {
  final byte[] b = new byte[IPV6_BIT_SIZE / 8];
  final int start = (b.length - 1) * 8;
  for (int i = 0; i < b.length; i++) {
    int shift = start - i * 8;
    if (shift > 0)
      b[i] = l.shiftRight(start - i * 8).byteValue();
    else
      b[i] = l.byteValue();
  }
  try {
    return (Inet6Address) Inet6Address.getByAddress(b);
  } catch (final UnknownHostException e) {
    throw new RuntimeException(e); // This should not happen, as we do not do a DNS lookup
  }
}
 
Example 2
Source File: IPAddressLargeDivisionGrouping.java    From IPAddress with Apache License 2.0 6 votes vote down vote up
@Override
protected byte[] getBytesImpl(boolean low) {
	byte bytes[] = new byte[(getBitCount() + 7) >> 3];
	int byteCount = bytes.length;
	int divCount = getDivisionCount();
	for(int k = divCount - 1, byteIndex = byteCount - 1, bitIndex = 8; k >= 0; k--) {
		IPAddressLargeDivision div = getDivision(k);
		BigInteger divValue = low ? div.getValue() : div.getUpperValue();
		int divBits = div.getBitCount();
		//write out this entire segment
		while(divBits > 0) {
			BigInteger bits = divValue.shiftLeft(8 - bitIndex);
			bytes[byteIndex] |= bits.byteValue();
			divValue = divValue.shiftRight(bitIndex);
			if(divBits < bitIndex) {
				bitIndex -= divBits;
				break;
			} else {
				divBits -= bitIndex;
				bitIndex = 8;
				byteIndex--;
			}
		}
	}
	return bytes;
}
 
Example 3
Source File: TrailingSignatureAlgorithm.java    From aws-encryption-sdk-java with Apache License 2.0 6 votes vote down vote up
/**
 * Encodes a compressed elliptic curve point as described in SEC-1 v2 section 2.3.3
 *
 * @param key The Elliptic Curve public key to compress and serialize
 * @return The serialized and compressed public key
 * @see <a href="http://www.secg.org/sec1-v2.pdf">http://www.secg.org/sec1-v2.pdf</a>
 */
@Override
public String serializePublicKey(PublicKey key) {
    notNull(key, "key is required");
    isInstanceOf(ECPublicKey.class, key, "key must be an instance of ECPublicKey");

    final BigInteger x = ((ECPublicKey) key).getW().getAffineX();
    final BigInteger y = ((ECPublicKey) key).getW().getAffineY();
    final BigInteger compressedY = y.mod(TWO).equals(ZERO) ? TWO : THREE;

    final byte[] xBytes = bigIntegerToByteArray(x,
            ecParameterSpec.getCurve().getField().getFieldSize() / Byte.SIZE);

    final byte[] compressedKey = new byte[xBytes.length + 1];
    System.arraycopy(xBytes, 0, compressedKey, 1, xBytes.length);
    compressedKey[0] = compressedY.byteValue();

    return encodeBase64String(compressedKey);
}
 
Example 4
Source File: OperatorUtils.java    From hasor with Apache License 2.0 6 votes vote down vote up
private static Number newReal(int realType, BigInteger value) {
    switch (realType) {
    case BOOL:
        return BigInteger.ZERO.compareTo(value) == 0 ? 0 : 1;
    case BYTE:
        return value.byteValue();
    case SHORT:
        return value.shortValue();
    case CHAR:
    case INT:
        return value.intValue();
    case LONG:
        return value.longValue();
    default:
        return value;
    }
}
 
Example 5
Source File: ByteType.java    From headlong with Apache License 2.0 5 votes vote down vote up
@Override
Byte decode(ByteBuffer bb, byte[] unitBuffer) {
    bb.get(unitBuffer);
    BigInteger bi = new BigInteger(unitBuffer);
    validateBigInt(bi);
    return bi.byteValue();
}
 
Example 6
Source File: Conversion.java    From chvote-protocol-poc with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Algorithm 4.4: ToByteArray
 *
 * @param x the integer to be converted
 * @param n the target length (in bytes)
 * @return the converted value, left-padded with <tt>0</tt>s if length is smaller than target, or trucated left if its larger
 */
public byte[] toByteArray(BigInteger x, int n) {
    Preconditions.checkArgument(x.signum() >= 0, "x must be non-negative");
    Preconditions.checkArgument(n >= (int) Math.ceil(x.bitLength() / 8.0));
    byte[] byteArray = new byte[n];

    BigInteger current = x;
    for (int i = 1; i <= n; i++) {
        byteArray[n - i] = current.byteValue(); // = current.mod(256)
        current = current.shiftRight(8); // current.divide(256)
    }

    return byteArray;
}
 
Example 7
Source File: TlvDecoder.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Decodes a byte array into an integer value.
 */
public static Number decodeInteger(byte[] value) throws TlvException {
    BigInteger bi = new BigInteger(value);
    if (value.length == 1) {
        return bi.byteValue();
    } else if (value.length <= 2) {
        return bi.shortValue();
    } else if (value.length <= 4) {
        return bi.intValue();
    } else if (value.length <= 8) {
        return bi.longValue();
    } else {
        throw new TlvException("Invalid length for an integer value: " + value.length);
    }
}
 
Example 8
Source File: TlvDecoder.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Decodes a byte array into an integer value.
 */
public static Number decodeInteger(byte[] value) throws TlvException {
    BigInteger bi = new BigInteger(value);
    if (value.length == 1) {
        return bi.byteValue();
    } else if (value.length <= 2) {
        return bi.shortValue();
    } else if (value.length <= 4) {
        return bi.intValue();
    } else if (value.length <= 8) {
        return bi.longValue();
    } else {
        throw new TlvException("Invalid length for an integer value: " + value.length);
    }
}
 
Example 9
Source File: CryptoNoteAddressValidator.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void decodeChunk(String input,
                                int inputOffset,
                                int inputLength,
                                byte[] decoded,
                                int decodedOffset,
                                int decodedLength) throws Exception {

    BigInteger result = BigInteger.ZERO;

    BigInteger order = BigInteger.ONE;
    for (int index = inputOffset + inputLength; index != inputOffset; order = order.multiply(ALPHABET_SIZE)) {
        char character = input.charAt(--index);
        int digit = ALPHABET.indexOf(character);
        if (digit == -1) {
            throw new Exception("invalid character " + character);
        }
        result = result.add(order.multiply(BigInteger.valueOf(digit)));
        if (result.compareTo(UINT64_MAX) > 0) {
            throw new Exception("64-bit unsigned integer overflow " + result.toString());
        }
    }

    BigInteger maxCapacity = BigInteger.ONE.shiftLeft(8 * decodedLength);
    if (result.compareTo(maxCapacity) >= 0) {
        throw new Exception("capacity overflow " + result.toString());
    }

    for (int index = decodedOffset + decodedLength; index != decodedOffset; result = result.shiftRight(8)) {
        decoded[--index] = result.byteValue();
    }
}
 
Example 10
Source File: Main.java    From Java-Coding-Problems with MIT License 3 votes vote down vote up
public static void main(String[] args) {

        BigInteger nr = BigInteger.valueOf(Long.MAX_VALUE);
               
        long nrLong = nr.longValue();        
        System.out.println(nr + " as long is: " + nrLong);
        
        int nrInt = nr.intValue();
        System.out.println(nr + " as int is: " + nrInt);
        
        short nrShort = nr.shortValue();
        System.out.println(nr + " as short is: " + nrShort);
        
        byte nrByte = nr.byteValue();                                
        System.out.println(nr + " as byte is: " + nrByte);
                
        long nrExactLong = nr.longValueExact(); // ok       
        System.out.println(nr + " as exact long is: " + nrExactLong);
        
        int nrExactInt = nr.intValueExact(); // ArithmeticException
        System.out.println(nr + " as exact int is: " + nrExactInt);
        
        short nrExactShort = nr.shortValueExact(); // ArithmeticException
        System.out.println(nr + " as exact short is: " + nrExactShort);
        
        byte nrExactByte = nr.byteValueExact(); // ArithmeticException                        
        System.out.println(nr + " as exact byte is: " + nrExactByte);
    }
 
Example 11
Source File: UByte.java    From CPE552-Java with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Throw exception if value out of range (long version)
 *
 * @param value Value to check
 * @return value if it is in range
 * @throws ArithmeticException if value is out of range
 */
public static byte checkSigned(BigInteger value) throws ArithmeticException {
    if (value.compareTo(BigInteger.ZERO) < 0 || value.intValue() > MAX_VALUE) {
        throw new ArithmeticException("Value is out of range : " + value);
    }
    return value.byteValue();
}
 
Example 12
Source File: UByte.java    From glm with MIT License 3 votes vote down vote up
/**
 * Throw exception if value out of range (long version)
 *
 * @param value Value to check
 * @return value if it is in range
 * @throws ArithmeticException if value is out of range
 */
public static byte checkSigned(BigInteger value) throws ArithmeticException {
    if (value.compareTo(BigInteger.ZERO) < 0 || value.intValue() > MAX_VALUE) {
        throw new ArithmeticException("Value is out of range : " + value);
    }
    return value.byteValue();
}
 
Example 13
Source File: UByte.java    From CPE552-Java with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Create an <code>unsigned byte</code>
 *
 * @param value
 */
public UByte(BigInteger value) {
    this.value = value.byteValue();
}
 
Example 14
Source File: UByte.java    From glm with MIT License 2 votes vote down vote up
/**
 * Create an <code>unsigned byte</code>
 *
 * @param value
 */
public UByte(BigInteger value) {
    this.value = value.byteValue();
}