vite.utils.ByteUtil Maven / Gradle / Ivy
The newest version!
package vite.utils;
import org.bouncycastle.util.encoders.Hex;
import java.math.BigInteger;
import java.util.Arrays;
public class ByteUtil {
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
public static byte[] bigInttoBytes(BigInteger bigInteger) {
byte[] tmp = bigInteger.toByteArray();
byte[] res = new byte[tmp.length - 1];
if (tmp[0] == 0) {
System.arraycopy(tmp, 1, res, 0, res.length);
return res;
}
return tmp;
}
/**
* The regular {@link BigInteger#toByteArray()} method isn't quite what we often need:
* it appends a leading zero to indicate that the number is positive and may need padding.
*
* @param b the integer to format into a byte array
* @param numBytes the desired size of the resulting byte array
* @return numBytes byte long array.
*/
public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) {
if (b == null)
return null;
byte[] bytes = new byte[numBytes];
byte[] biBytes = b.toByteArray();
int start = (biBytes.length == numBytes + 1) ? 1 : 0;
int length = Math.min(biBytes.length, numBytes);
System.arraycopy(biBytes, start, bytes, numBytes - length, length);
return bytes;
}
public static byte[] bigIntegerToBytesSigned(BigInteger b, int numBytes) {
if (b == null)
return null;
byte[] bytes = new byte[numBytes];
Arrays.fill(bytes, b.signum() < 0 ? (byte) 0xFF : 0x00);
byte[] biBytes = b.toByteArray();
int start = (biBytes.length == numBytes + 1) ? 1 : 0;
int length = Math.min(biBytes.length, numBytes);
System.arraycopy(biBytes, start, bytes, numBytes - length, length);
return bytes;
}
public static BigInteger bytesToBigInteger(byte[] bb) {
return (bb == null || bb.length == 0) ? BigInteger.ZERO : new BigInteger(1, bb);
}
public static byte[] and(byte[] b1, byte[] b2) {
if (b1.length != b2.length) throw new RuntimeException("Array sizes differ");
byte[] ret = new byte[b1.length];
for (int i = 0; i < ret.length; i++) {
ret[i] = (byte) (b1[i] & b2[i]);
}
return ret;
}
public static byte[] or(byte[] b1, byte[] b2) {
if (b1.length != b2.length) throw new RuntimeException("Array sizes differ");
byte[] ret = new byte[b1.length];
for (int i = 0; i < ret.length; i++) {
ret[i] = (byte) (b1[i] | b2[i]);
}
return ret;
}
/**
* @param arrays - arrays to merge
* @return - merged array
*/
public static byte[] merge(byte[]... arrays) {
int count = 0;
for (byte[] array : arrays) {
count += array.length;
}
// Create new array and copy all array contents
byte[] mergedArray = new byte[count];
int start = 0;
for (byte[] array : arrays) {
System.arraycopy(array, 0, mergedArray, start, array.length);
start += array.length;
}
return mergedArray;
}
/**
* Convert a byte-array into a hex String.
*/
public static String toHexString(byte[] data) {
return data == null ? "" : Hex.toHexString(data);
}
public static byte[] hexStringToBytes(String data) {
if (data == null) return EMPTY_BYTE_ARRAY;
if (data.startsWith("0x")) data = data.substring(2);
if (data.length() % 2 == 1) data = "0" + data;
return Hex.decode(data);
}
public static byte[] negation(byte[] data) {
for(int i=0;i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy