Convert long to byte array and add it to another array

I want to change the values ​​in a byte array to put a long timestamp value in MSB. Can someone tell me the best way to do this. I don’t want to insert bit-by-bit values, which I think are very inefficient.

long time = System.currentTimeMillis(); Long timeStamp = new Long(time); byte[] bArray = new byte[128]; 

I want something like:

 byte[0-63] = timeStamp.byteValue(); 

It is possible. What is the best way to edit / paste values ​​into this byte array. since the byte is primitive, I don’t think there are some direct implementations that I can use?

Edit:
It seems that System.currentTimeMillis() faster than Calendar.getTimeInMillis() , therefore replacing the above code with it. Please correct me if you are mistaken.

+44
java long-integer type-conversion bytearray
Nov 28 '10 at
source share
5 answers

There are several ways to do this:

  • Use ByteBuffer (the best option is 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 it manually (taken from 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 add these bytes to your existing array with 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]; } 
+120
Nov 28 2018-10-28
source share

If you really want to 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 }; } 
+17
May 10 '14 at 18:33
source share

For me, ByteBuffer and other utilities are expensive in terms of time. Here are two methods you can use:

// first method (which uses the second method), returns an array allocated and executed

 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 a buffer and want to write a long 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); } } 
+3
Jul 12 '16 at 18:58
source share

It doesn't look like you can slice an array of bytes to insert something into a subset without making a byte a byte. Look Take an array segment in Java without creating a new array on the heap . Basically what I would like to do is set up an array of 64 bytes and set the time for it, and then add an empty 64-byte array to it. Or just make it byte by byte.

0
Nov 28 '10 at 21:24
source share

I am updating this post because I just announced a preliminary version of the library that will convert longs to byte arrays (and vice versa). The library is very small and converts any java primitive to an array of bytes.

http://rschilling.wordpress.com/2013/09/26/pre-release-announcement-pend-oreille/ http://code.google.com/p/pend-oreille/

If you use it, you can do things like convert long arrays to byte arrays:

 Double[] doubles = new Double[1000]; for (int i = 2; i < 1002; i++) { doubles[i - 2] = (double) i; } byte[] resultBytes1 = (byte[]) new PrimitiveHelper(PrimitiveUtil.unbox(doubles)) .asType(byte[].class); 

You can also convert one long value.

 byte[] resultBytes1 = (byte[]) new PrimitiveHelper(1000l) .asType(byte[].class); 

Feel free to provide some feedback.

October 4, 2013 Patch: I now released the creation of the library http://rschilling.wordpress.com/2013/10/04/pend-oreille-official-1-0-release/

0
Sep 26 '13 at 4:22
source share



All Articles