51

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 that System.currentTimeMillis() is faster than Calendar.getTimeInMillis(), so replacing the above code by it.Please correct me if wrong.

Jonas
  • 121,568
  • 97
  • 310
  • 388
codeObserver
  • 6,521
  • 16
  • 76
  • 121
  • This was helpful: http://stackoverflow.com/questions/5399798/byte-array-and-int-conversion-in-java – TacB0sS Jun 03 '12 at 13:30
  • I think you have the indices count wrong, check this question out: http://stackoverflow.com/questions/5399798/byte-array-and-int-conversion-in-java – TacB0sS Jun 03 '12 at 13:45

5 Answers5

136

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];
}
Sebastian S
  • 4,420
  • 4
  • 34
  • 63
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • thanks 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: http://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 ? – codeObserver Nov 28 '10 at 21:36
  • Use `ByteBuffer`. I doubt it will cause any problems. – Bozho Nov 28 '10 at 21:46
  • 5
    I 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(); – mR_fr0g Sep 07 '11 at 16:41
  • 2
    I assume Java is [generally big endian](http://stackoverflow.com/questions/981549/javas-virtual-machines-endianness), while x86 is little endian. – white_gecko Nov 02 '11 at 17:36
  • 5
    To avoid hardcoding the size of a long I would use `allocate(Long.SIZE)` – mike jones Feb 05 '13 at 18:13
  • 7
    @mikejones Note that Long.SIZE will give you the number of bits used to represent Long (http://docs.oracle.com/javase/6/docs/api/java/lang/Long.html#SIZE). You'd need (Long.SIZE / Byte.SIZE) to match the example above. – overthink May 02 '13 at 16:02
  • 3
    @overthink Java 8 introduced a BYTES constant for this (http://docs.oracle.com/javase/8/docs/api/java/lang/Long.html#BYTES). – Max Jan 17 '15 at 18:44
22

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
    };
}
cr0ck3t
  • 341
  • 2
  • 8
  • From some testing of my own, the above longToByteArray solution executed about 5 times faster than ByteBuffer.allocate.putLong.array. – mcfisty Dec 19 '22 at 09:40
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);
    }
}
Colateral
  • 1,736
  • 2
  • 18
  • 22
0

I am updating this post because I have just announced a pre-release version of a library that will convert longs to byte arrays (and back again). The library is very small and will convert any java primitive to a byte array.

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 a single long value as well.

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

Feel free to provide some feedback.

Update on October 4, 2013: I've now released the production of the library http://rschilling.wordpress.com/2013/10/04/pend-oreille-official-1-0-release/

Coder Roadie
  • 838
  • 1
  • 8
  • 11
0

It doesn't look like you can slice a byte array to insert something into a subset without doing it byte by byte. Look at Grab a segment of an array in Java without creating a new array on heap . Basically what I would do is set create a 64 byte array and set the time to it then append a blank 64 byte array to it. Or just do it byte by byte.

Community
  • 1
  • 1
Matt Phillips
  • 11,249
  • 10
  • 46
  • 71