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

发布于 2024-10-05 01:37:01 字数 479 浏览 0 评论 0原文

我想更改字节数组中的值以将长时间戳值放入 M​​SB 中。有人能告诉我最好的方法是什么吗?我不想一点一点地插入值,我认为这是非常低效的。

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

我想要的是这样的东西:

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

这样的事情可能吗?在此字节数组中编辑/插入值的最佳方法是什么?由于字节是一个原语,我不认为有一些我可以使用的直接实现?

编辑:
看来System.currentTimeMillis()Calendar.getTimeInMillis()更快,所以用它替换上面的代码。如果错误,请纠正我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

画▽骨i 2024-10-12 01:37:02

有多种方法可以做到这一点:

  • 使用 ByteBuffer(最佳选择 - 简洁且易于阅读):

    byte[] bytes = ByteBuffer.allocate(Long.SIZE / Byte.SIZE).putLong(someLong).array();
    
  • 您还可以使用DataOutputStream(更详细):

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.writeLong(someLong);
    dos.close();
    byte[] longBytes = baos.toByteArray();
    
  • 最后,您可以手动执行此操作(取自 Hector 代码中的 LongSerializer)(更难阅读):

    byte[] b = 新字节[8];
    for (int i = 0; i < 大小; ++i) {
      b[i] = (字节) (l >> (大小 - i - 1 << 3));
    }
    

然后您可以通过简单的方法将这些字节附加到现有数组中环形:

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

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];
}
中二柚 2024-10-12 01:37:02

如果你想真正深入了解......

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

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
    };
}
昔日梦未散 2024-10-12 01:37:02

对我来说,从时间角度来看,ByteBuffer 和其他实用程序都很昂贵。您可以使用以下两种方法:

// 第一种方法(即使用第二种方法),它返回分配并满足的数组

public byte[] longToByteArray(long value) 
{
        byte[] array = new byte[8];

        longToByteArray(value,array,0);
        return array;
}

// 如果您已经分配了缓冲区并且想要写入 long a,则此方法很有用数组中的特定位置。

public void longToByteArray(long value, byte[] array, int startFrom) 
{
    for (int i=7; i>=0; i--)
    {
        array[startFrom+7-i] = (byte) (value >> i*8);
    }
}

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);
    }
}
通知家属抬走 2024-10-12 01:37:02

看起来您无法对字节数组进行切片以将某些内容插入到子集中,而无需逐字节进行操作。查看 在 Java 中抓取数组的一段无需在堆上创建新数组 。基本上我要做的就是创建一个 64 字节数组并为其设置时间,然后向其附加一个空白的 64 字节数组。或者只是逐个字节地进行。

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.

一身软味 2024-10-12 01:37:02

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

http://rschilling.wordpress.com/2013/ 09/26/预发布-公告-pend-oreille/
http://code.google.com/p/pend-oreille/

如果您使用它你可以做一些事情,比如将长数组转换为字节数组:

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

你也可以转换单个长值。

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

请随时提供一些反馈。

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

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文