我有一个大小为8的byteArray
,其中包含一个40位的密钥,我想要添加0填充直到它成为56位。
byte[] aKey = new byte [8]; // How I instantiated my byte array
有什么想法吗?(涉及IT技术)
我有一个大小为8的byteArray
,其中包含一个40位的密钥,我想要添加0填充直到它成为56位。
byte[] aKey = new byte [8]; // How I instantiated my byte array
byte[] aKey = new byte [8]
byte[] b = new byte[8];
BitSet bitSet = BitSet.valueOf(b);
bitSet.clear(41, 56); //This will clear 41st to 56th Bit
b = bitSet.toByteArray();
BitSet.valueOf(byte[])
和BitSet.toByteArray()
。static final int PADDING_SIZE = 2;
public static void main(String[] args) {
byte[] aKey = {1, 2, 3, 4, 5, 6, 7, 8}; // your array of size 8
System.out.println(Arrays.toString(aKey));
byte[] newKey = new byte[8];
System.arraycopy(aKey, 0, newKey, PADDING_SIZE, aKey.length - PADDING_SIZE); // right shift
System.out.println(Arrays.toString(newKey));
}
printArray(byte[] array)
中使用 System.out.println(Arrays.toString(array));
来打印一个数组。 - Srikanth Reddy LingalaGuava的com.google.common.primitives.Bytes.ensureCapacity
:
aKey = Bytes.ensureCapacity(aKey , 56/8, 0);
aKey = java.util.Arrays.copyOf(aKey , 56/8);