Reputation: 2333
I got an integer: 1695609641
when I use method:
String hex = Integer.toHexString(1695609641);
system.out.println(hex);
gives:
6510f329
but I want a byte array:
byte[] bytearray = new byte[] { (byte) 0x65, (byte)0x10, (byte)0xf3, (byte)0x29};
How can I make this?
Upvotes: 233
Views: 471568
Reputation: 19
I came here looking for the best way to do this, but then decided to go with:
String.valueOf(<your-int>).getBytes()
It was simpler for my purposes (writing an int to a file). Not to mention that when encoding you can use less than 4 bytes for small values (ascii) vs. encoding a 32-bit int.
Upvotes: 0
Reputation: 180
Here are two function to do the mirrored thing:
byte[] intToBytes(int i) {
return new byte[]{
(byte) (i >>> 24),
(byte) (i >>> 16),
(byte) (i >>> 8),
(byte) i
};
}
int bytesToInt(byte[] b) {
//Cuz int encode by complement-on-two
//For a negative, signed left shift operation will Fill the upper part of the binary with 1.
//That‘s a question for us to combine the meaningful part.
//Here, we execute a AND 0xFF operation, to implicitly convert a byte to int, and fill the upper part of the binary with 0
//So ,we got a positive number now.
//The next step just execute OR operation to combine the four part as an integer.
return (b[0]) << 24 |
(b[1] & 0xFF) << 16 |
(b[2] & 0xFF) << 8 |
(b[3] & 0xFF);
}
Upvotes: 0
Reputation: 1065
byte[] IntToByteArray( int data ) {
byte[] result = new byte[4];
result[0] = (byte) ((data & 0xFF000000) >> 24);
result[1] = (byte) ((data & 0x00FF0000) >> 16);
result[2] = (byte) ((data & 0x0000FF00) >> 8);
result[3] = (byte) ((data & 0x000000FF) >> 0);
return result;
}
Upvotes: 34
Reputation: 2558
If you're using apache-commons
public static byte[] toByteArray(int value) {
byte result[] = new byte[4];
return Conversion.intToByteArray(value, 0, result, 0, 4);
}
Upvotes: 5
Reputation: 550
Because generally you would want to convert this array back to an int at a later point, here are the methods to convert an array of ints into an array of bytes and vice-versa:
public static byte[] convertToByteArray(final int[] pIntArray)
{
final byte[] array = new byte[pIntArray.length * 4];
for (int j = 0; j < pIntArray.length; j++)
{
final int c = pIntArray[j];
array[j * 4] = (byte)((c & 0xFF000000) >> 24);
array[j * 4 + 1] = (byte)((c & 0xFF0000) >> 16);
array[j * 4 + 2] = (byte)((c & 0xFF00) >> 8);
array[j * 4 + 3] = (byte)(c & 0xFF);
}
return array;
}
public static int[] convertToIntArray(final byte[] pByteArray)
{
final int[] array = new int[pByteArray.length / 4];
for (int i = 0; i < array.length; i++)
array[i] = (((int)(pByteArray[i * 4]) << 24) & 0xFF000000) |
(((int)(pByteArray[i * 4 + 1]) << 16) & 0xFF0000) |
(((int)(pByteArray[i * 4 + 2]) << 8) & 0xFF00) |
((int)(pByteArray[i * 4 + 3]) & 0xFF);
return array;
}
Note that because of sign propagation and such, the "& 0xFF..." are needed when converting back to the int.
Upvotes: 4
Reputation: 23505
Using Guava:
byte[] bytearray = Ints.toByteArray(1695609641);
Upvotes: 29
Reputation: 1638
My try :
public static byte[] toBytes(final int intVal, final int... intArray) {
if (intArray == null || (intArray.length == 0)) {
return ByteBuffer.allocate(4).putInt(intVal).array();
} else {
final ByteBuffer bb = ByteBuffer.allocate(4 + (intArray.length * 4)).putInt(intVal);
for (final int val : intArray) {
bb.putInt(val);
}
return bb.array();
}
}
With it you can do this :
byte[] fourBytes = toBytes(0x01020304);
byte[] eightBytes = toBytes(0x01020304, 0x05060708);
Full class is here : https://gist.github.com/superbob/6548493, it supports initialization from shorts or long
byte[] eightBytesAgain = toBytes(0x0102030405060708L);
Upvotes: 1
Reputation: 89
public static byte[] intToBytes(int x) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bos);
out.writeInt(x);
out.close();
byte[] int_bytes = bos.toByteArray();
bos.close();
return int_bytes;
}
Upvotes: 7
Reputation: 12350
The chunks below work at least for sending an int over UDP.
int to byte array:
public byte[] intToBytes(int my_int) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeInt(my_int);
out.close();
byte[] int_bytes = bos.toByteArray();
bos.close();
return int_bytes;
}
byte array to int:
public int bytesToInt(byte[] int_bytes) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(int_bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
int my_int = ois.readInt();
ois.close();
return my_int;
}
Upvotes: 2
Reputation: 777
The class org.apache.hadoop.hbase.util.Bytes has a bunch of handy byte[] conversion methods, but you might not want to add the whole HBase jar to your project just for this purpose. It's surprising that not only are such method missing AFAIK from the JDK, but also from obvious libs like commons io.
Upvotes: 1
Reputation: 116304
using Java NIO's ByteBuffer is very simple:
byte[] bytes = ByteBuffer.allocate(4).putInt(1695609641).array();
for (byte b : bytes) {
System.out.format("0x%x ", b);
}
output:
0x65 0x10 0xf3 0x29
Upvotes: 354
Reputation: 67750
byte[] conv = new byte[4];
conv[3] = (byte) input & 0xff;
input >>= 8;
conv[2] = (byte) input & 0xff;
input >>= 8;
conv[1] = (byte) input & 0xff;
input >>= 8;
conv[0] = (byte) input;
Upvotes: 7
Reputation: 24251
How about:
public static final byte[] intToByteArray(int value) {
return new byte[] {
(byte)(value >>> 24),
(byte)(value >>> 16),
(byte)(value >>> 8),
(byte)value};
}
The idea is not mine. I've taken it from some post on dzone.com.
Upvotes: 189
Reputation: 272217
integer & 0xFF
for the first byte
(integer >> 8) & 0xFF
for the second and loop etc., writing into a preallocated byte array. A bit messy, unfortunately.
Upvotes: 2