Java 如何向字节数组添加填充?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19474957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add padding on to a byte array?
提问by Napmi
I have this 40 bitkey in a byteArray
of size 8, and I want to add 0 padding to it until it becomes 56 bit.
我有一个大小为8 的40 位密钥,我想向它添加 0 填充直到它变成56 位。byteArray
byte[] aKey = new byte [8]; // How I instantiated my byte array
Any ideas how?
任何想法如何?
采纳答案by Srikanth Reddy Lingala
An 8 byte array is of 64 bits. If you initialize the array as
一个 8 字节的数组是 64 位的。如果您将数组初始化为
byte[] aKey = new byte [8]
all bytes are initialized with 0's. If you set the first 40 bits, that is 5 bytes, then your other 3 bytes, i.e, from 41 to 64 bits are still set to 0. So, you have by default from 41st bit to 56th bit set to 0 and you don't have to reset them.
所有字节都用 0 初始化。如果您设置前 40 位,即 5 个字节,那么您的其他 3 个字节,即从 41 到 64 位仍设置为 0。因此,默认情况下,您将第 41 位到第 56 位设置为 0 而您不不必重置它们。
However, if your array is already initialized with some values and you want to clear the bits from 41 to 56, there are a few ways to do that.
但是,如果您的数组已经用一些值初始化并且您想清除 41 到 56 位,有几种方法可以做到这一点。
First:you can just set aKey[5] = 0
and aKey[6] = 0
This will set the 6th bye and the 7th byte, which make up from 41st to 56th bit, to 0
第一:你可以只设置aKey[5] = 0
andaKey[6] = 0
这将把第 6 个字节和第 7 个字节,从第 41 位到第 56 位,设置为 0
Second:If you are dealing with bits, you can also use BitSet. However, in your case, I see first approach much easier, especially, if you are pre Java 7, some of the below methods do not exist and you have to write your own methodsto convert from byte array to bit set and vice-versa.
第二:如果是处理bits,也可以使用BitSet。但是,就您而言,我认为第一种方法要容易得多,特别是,如果您使用的是 Java 7 之前的版本,则以下某些方法不存在,您必须编写自己的方法来将字节数组转换为位集,反之亦然.
byte[] b = new byte[8];
BitSet bitSet = BitSet.valueOf(b);
bitSet.clear(41, 56); //This will clear 41st to 56th Bit
b = bitSet.toByteArray();
Note: BitSet.valueOf(byte[])
and BitSet.toByteArray()
exists only from Java 7.
注意:BitSet.valueOf(byte[])
并且BitSet.toByteArray()
仅在 Java 7 中存在。
回答by Stanislav Mamontov
Use System.arraycopy() to insert two bytes (56-40 = 16 bit) at the start of your array.
使用 System.arraycopy() 在数组的开头插入两个字节(56-40 = 16 位)。
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));
}
回答by 30thh
Guava's com.google.common.primitives.Bytes.ensureCapacity
:
番石榴 com.google.common.primitives.Bytes.ensureCapacity
:
aKey = Bytes.ensureCapacity(aKey , 56/8, 0);
or since JDK6 using Java native tools:
或自 JDK6 使用 Java 本机工具:
aKey = java.util.Arrays.copyOf(aKey , 56/8);