Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert long to byte array and add it to another array

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.

like image 235
codeObserver Avatar asked Nov 28 '10 21:11

codeObserver


People also ask

How do you convert bytes to long objects?

The BigInteger class has a longValue() method to convert a byte array to a long value: long value = new BigInteger(bytes). longValue();

Can we convert string to byte array in Java?

The String class provides three overloaded getBytes methods to encode a String into a byte array: getBytes() – encodes using platform's default charset. getBytes (String charsetName) – encodes using the named charset. getBytes (Charset charset) – encodes using the provided charset.

How do you split byteArray?

Split Byte Array In Java, we can use ByteBuffer or System. arraycopy to split a single byte array into multiple byte arrays. For example, this 000102030a0b0c0d1a1b1c1d2f2f is a byte array (14 bytes) in hex representation, it is a combined of cipher (8 bytes) + nonce (4 bytes) + extra (2 bytes).


2 Answers

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];
}
like image 189
Bozho Avatar answered Oct 23 '22 03:10

Bozho


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
    };
}
like image 21
cr0ck3t Avatar answered Oct 23 '22 03:10

cr0ck3t