Convert long to byte array and add it to another array
Solution 1
There are multiple ways to do it:
-
Use a
ByteBuffer
(best option - concise and easy to read):byte[] bytes = ByteBuffer.allocate(Long.SIZE / Byte.SIZE).putLong(someLong).array();
-
You can also use
DataOutputStream
(more verbose):ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeLong(someLong); dos.close(); byte[] longBytes = baos.toByteArray();
-
Finally, you can do this manually (taken from the
LongSerializer
in Hector's code) (harder to read):byte[] b = new byte[8]; for (int i = 0; i < size; ++i) { b[i] = (byte) (l >> (size - i - 1 << 3)); }
Then you can append these bytes to your existing array by a simple loop:
// change this, if you want your long to start from
// a different position in the array
int start = 0;
for (int i = 0; i < longBytes.length; i ++) {
bytes[start + i] = longBytes[i];
}
Solution 2
If you want to really get under the hood...
public byte[] longToByteArray(long value) {
return new byte[] {
(byte) (value >> 56),
(byte) (value >> 48),
(byte) (value >> 40),
(byte) (value >> 32),
(byte) (value >> 24),
(byte) (value >> 16),
(byte) (value >> 8),
(byte) value
};
}
Solution 3
For me ByteBuffer and other utils are expensive from time perspective. Here are 2 methods that you can use:
// first method (that is using the second method), it return the array allocated and fulfilled
public byte[] longToByteArray(long value)
{
byte[] array = new byte[8];
longToByteArray(value,array,0);
return array;
}
// this method is useful if you have already allocated the buffer and you want to write the long a specific location in the array.
public void longToByteArray(long value, byte[] array, int startFrom)
{
for (int i=7; i>=0; i--)
{
array[startFrom+7-i] = (byte) (value >> i*8);
}
}
codeObserver
Updated on July 13, 2020Comments
-
codeObserver almost 4 years
I want to change a values in byte array to put a long timestamp value in in the MSBs. Can someone tell me whats the best way to do it. I do not want to insert values bit-by-bit which I believe is very inefficient.
long time = System.currentTimeMillis(); Long timeStamp = new Long(time); byte[] bArray = new byte[128];
What I want is something like:
byte[0-63] = timeStamp.byteValue();
Is something like this possible . What is the best way to edit/insert values in this byte array. since byte is a primitive I dont think there are some direct implementations I can make use of?
Edit:
It seems thatSystem.currentTimeMillis()
is faster thanCalendar.getTimeInMillis()
, so replacing the above code by it.Please correct me if wrong. -
codeObserver over 13 yearsthanks Bozho. I think this is what I was looking for . I think the first method would be faster than the later one. i am just concerned about the performance impact of using a ByteBuffer instead of byte array. This link talks more about the performance in the ned bytebuffer lib: jroller.com/cpurdy/date/20040405#raw_nio_performance ; Also if the impact is not huge, can I just use ByteBuffer instead of a byte array ?
-
Bozho over 13 yearsUse
ByteBuffer
. I doubt it will cause any problems. -
mR_fr0g over 12 yearsI found that I had to set the order on the ByteByffer like so, Otherwise the bytes were the in an order reverse to what i was expecting byte[] bytes = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(bits).array();
-
white_gecko over 12 yearsI assume Java is generally big endian, while x86 is little endian.
-
mike jones about 11 yearsTo avoid hardcoding the size of a long I would use
allocate(Long.SIZE)
-
overthink almost 11 years@mikejones Note that Long.SIZE will give you the number of bits used to represent Long (docs.oracle.com/javase/6/docs/api/java/lang/Long.html#SIZE). You'd need (Long.SIZE / Byte.SIZE) to match the example above.
-
Max over 9 years@overthink Java 8 introduced a BYTES constant for this (docs.oracle.com/javase/8/docs/api/java/lang/Long.html#BYTES).