将long转换为字节数组并将其添加到另一个数组

时间:2022-07-21 16:38:16

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.

我想要更改字节数组中的值,以便在MSBs中放入一个较长的时间戳值。谁能告诉我最好的方法是什么?我不希望逐位插入值,我认为这非常低效。

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.

编辑:似乎System.currentTimeMillis()比Calendar.getTimeInMillis()要快,所以用它替换上面的代码。请纠正我的错误。

5 个解决方案

#1


123  

There are multiple ways to do it:

有多种方法可以做到:

  • Use a ByteBuffer (best option - concise and easy to read):

    使用ByteBuffer(最佳选项-简明易读):

    byte[] bytes = ByteBuffer.allocate(Long.SIZE / Byte.SIZE).putLong(someLong).array();
    
  • You can also use DataOutputStream (more verbose):

    您还可以使用DataOutputStream(更详细):

    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):

    最后,您可以手动操作(从Hector的代码中的LongSerializer中获取)(更难阅读):

    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];
}

#2


18  

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
    };
}

#3


3  

For me ByteBuffer and other utils are expensive from time perspective. Here are 2 methods that you can use:

对我来说ByteBuffer和其他util从时间的角度来看是很昂贵的。这里有两种方法你可以使用:

// 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);
    }
}

#4


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.

看起来不像你可以分割一个字节数组来插入一些东西到一个子集而不做一个字节一个字节。在Java中获取一个数组的片段,而不创建堆上的新数组。基本上,我要做的就是创建一个64字节数组并设置时间,然后添加一个64字节的空数组。或者一字节一字节地做。

#5


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.

我之所以更新这篇文章,是因为我刚刚发布了一个预发布版本的库,它将把长字符转换成字节数组(然后再转换回来)。该库非常小,可以将任何java原语转换为字节数组。

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

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/

2013年10月4日更新:我现在发布了库http://rschilling.wordpress.com/2013/10/04/pend- oreillie -official-1-0 release/

#1


123  

There are multiple ways to do it:

有多种方法可以做到:

  • Use a ByteBuffer (best option - concise and easy to read):

    使用ByteBuffer(最佳选项-简明易读):

    byte[] bytes = ByteBuffer.allocate(Long.SIZE / Byte.SIZE).putLong(someLong).array();
    
  • You can also use DataOutputStream (more verbose):

    您还可以使用DataOutputStream(更详细):

    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):

    最后,您可以手动操作(从Hector的代码中的LongSerializer中获取)(更难阅读):

    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];
}

#2


18  

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
    };
}

#3


3  

For me ByteBuffer and other utils are expensive from time perspective. Here are 2 methods that you can use:

对我来说ByteBuffer和其他util从时间的角度来看是很昂贵的。这里有两种方法你可以使用:

// 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);
    }
}

#4


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.

看起来不像你可以分割一个字节数组来插入一些东西到一个子集而不做一个字节一个字节。在Java中获取一个数组的片段,而不创建堆上的新数组。基本上,我要做的就是创建一个64字节数组并设置时间,然后添加一个64字节的空数组。或者一字节一字节地做。

#5


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.

我之所以更新这篇文章,是因为我刚刚发布了一个预发布版本的库,它将把长字符转换成字节数组(然后再转换回来)。该库非常小,可以将任何java原语转换为字节数组。

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

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/

2013年10月4日更新:我现在发布了库http://rschilling.wordpress.com/2013/10/04/pend- oreillie -official-1-0 release/