将整数转换为字节数组(Java)
将将整数转换为字节数组(Java),java,arrays,integer,byte,Java,Arrays,Integer,Byte,将整数转换为字节数组的快速方法是什么 e、 g.0xAABBCCDD=>{AA,BB,CC,DD}看看这个类 设置字节顺序可确保结果[0]==0xAA,结果[1]==0xBB,结果[2]==0xCC和结果[3]==0xDD 或者,您也可以手动执行此操作: byte[] toBytes(int i) { byte[] result = new byte[4]; result[0] = (byte) (i >> 24); result[1] = (byte) (i >
整数
转换为字节数组
的快速方法是什么
e、 g.0xAABBCCDD=>{AA,BB,CC,DD}
看看这个类
设置字节顺序可确保结果[0]==0xAA
,结果[1]==0xBB
,结果[2]==0xCC
和结果[3]==0xDD
或者,您也可以手动执行此操作:
byte[] toBytes(int i)
{
byte[] result = new byte[4];
result[0] = (byte) (i >> 24);
result[1] = (byte) (i >> 16);
result[2] = (byte) (i >> 8);
result[3] = (byte) (i /*>> 0*/);
return result;
}
然而,ByteBuffer
类是为这种脏手任务设计的。事实上,私有的java.nio.Bits
定义了ByteBuffer.putInt()所使用的这些辅助方法:
您可以使用biginger
:
private byte[] bigIntToByteArray( final int i ) {
BigInteger bigInt = BigInteger.valueOf(i);
return bigInt.toByteArray();
}
public byte[] intToBytes( final int i ) {
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(i);
return bb.array();
}
从整数:
byte[] array = BigInteger.valueOf(0xAABBCCDD).toByteArray();
System.out.println(Arrays.toString(array))
// --> {-86, -69, -52, -35 }
返回的数组的大小与表示数字所需的大小相同,因此它的大小可以是1,例如表示1。但是,如果传递int,则大小不能超过四个字节
从字符串:
BigInteger v = new BigInteger("AABBCCDD", 16);
byte[] array = v.toByteArray();
但是,您需要注意,如果第一个字节高于0x7F
(如本例所示),biginger将在数组的开头插入一个0x00字节。这是区分正值和负值所必需的。使用biginger
:
private byte[] bigIntToByteArray( final int i ) {
BigInteger bigInt = BigInteger.valueOf(i);
return bigInt.toByteArray();
}
public byte[] intToBytes( final int i ) {
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(i);
return bb.array();
}
使用DataOutputStream
:
private byte[] intToByteArray ( final int i ) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeInt(i);
dos.flush();
return bos.toByteArray();
}
使用ByteBuffer
:
private byte[] bigIntToByteArray( final int i ) {
BigInteger bigInt = BigInteger.valueOf(i);
return bigInt.toByteArray();
}
public byte[] intToBytes( final int i ) {
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(i);
return bb.array();
}
使用这个功能它对我有用
public byte[] toByteArray(int value) {
return new byte[] {
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)value};
}
它将int转换为字节值如果您愿意,可以使用它的类:
对于int
→ <代码>字节[]
,使用:
结果是{0xAA,0xBB,0xCC,0xDD}
其反面是或:
结果是
0xAABBCCDD
这里有一个方法,应该可以正确地完成这项工作
public byte[] toByteArray(int value)
{
final byte[] destination = new byte[Integer.BYTES];
for(int index = Integer.BYTES - 1; index >= 0; index--)
{
destination[i] = (byte) value;
value = value >> 8;
};
return destination;
};
这是我的解决方案:
public void getBytes(int val) {
byte[] bytes = new byte[Integer.BYTES];
for (int i = 0;i < bytes.length; i ++) {
int j = val % Byte.MAX_VALUE;
bytes[i] = (j == 0 ? Byte.MAX_VALUE : j);
}
}
这对你有帮助
import java.nio.ByteBuffer;
import java.util.Arrays;
public class MyClass
{
public static void main(String args[]) {
byte [] hbhbytes = ByteBuffer.allocate(4).putInt(16666666).array();
System.out.println(Arrays.toString(hbhbytes));
}
}
也可以改变-
byte[] ba = new byte[4];
int val = Integer.MAX_VALUE;
for(byte i=0;i<4;i++)
ba[i] = (byte)(val >> i*8);
//ba[3-i] = (byte)(val >> i*8); //Big-endian
byte[]ba=新字节[4];
int val=Integer.MAX_值;
用于(字节i=0;i>i*8);
//ba[3-i]=(字节)(val>>i*8)//大端
正确处理字节顺序的简单解决方案:
ByteBuffer.allocate(4).order(ByteOrder.nativeOrder()).putin(yourInt.array())代码>使用android非常简单
int i=10000;
byte b1=(byte)Color.alpha(i);
byte b2=(byte)Color.red(i);
byte b3=(byte)Color.green(i);
byte b4=(byte)Color.blue(i);
谢谢但是,既然这是BigInteger,ints是否正确环绕?这是Integer.MAX_值之外的整数,但仍然只能用4个字节表示?执行起来肯定不快。;)这不是一个好的选择。它不仅可以添加0x00字节,还可以去掉前导零。如果bytebuffer已经存在,这将很好地工作。。。否则,执行分配似乎比只分配一个长度为4的字节数组并手动执行移位需要更长的时间。。。但我们可能讨论的是一些细微的区别;在内部,它肯定是通过移位和掩蔽来实现的。这是一个完美的答案。请注意,big-endian是指定的默认值,并且方法是“可链接的”,position参数是可选的,因此它全部减少为:byte[]result=ByteBuffer.allocate(4).putInt(0xAABBCCDD.array();当然,如果您重复执行此操作并将所有结果连接在一起(这在执行此类操作时很常见),请分配一个缓冲区并重复将所需的所有内容放入其中--它将在执行过程中跟踪偏移量。它确实是一个非常有用的类。如果你打算在后续的NIO编写操作中使用这个ByteBuffer,你需要倒带它。否则,你什么都不会写!(去过那里,那样做了!)谁不知道呢。putInt将始终写入4个字节,无论输入整数的大小如何。如果您只需要2个字节,请使用putShort等…生成的字节数组的格式是否重要?你会怎么处理它?注意字节顺序,虽然ByteBuffer会给出一个无符号的int吗?@Pascal使用ByteBuffer我用ByteBuffer bb=ByteBuffer.allocate(3)试过;为此,它给出了java.nio.BufferOverflowException,我不明白为什么它的值小于4时不工作?您能解释一下吗?@SanjayJain您会遇到一个缓冲区溢出异常,因为Java中的int是32位或4字节大小,因此需要您在ByteBuffer中分配至少4字节的内存。@GregoryPakosz关于字节顺序的看法是正确的。如果你处理的是大于2^31-1的整数,那么他使用ByteBuffer
的答案会更直观。与其他答案相比,无论最重要的位是多少,这都是毫无意义的,而且效率更高。也可以使用“>>”。像这样的直接解决方案肯定比调用任何库方法都要快。有时,您只需直接用几行代码来处理这些位,而不必承担库方法调用的所有额外开销。这可以很好地在语言之间进行转换,所以有利于多语言软件开发。
int i=10000;
byte b1=(byte)Color.alpha(i);
byte b2=(byte)Color.red(i);
byte b3=(byte)Color.green(i);
byte b4=(byte)Color.blue(i);