将 long 转换为字节数组并将其添加到另一个数组
我想更改字节数组中的值以将长时间戳值放入 MSB 中。有人能告诉我最好的方法是什么吗?我不想一点一点地插入值,我认为这是非常低效的。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有多种方法可以做到这一点:
使用
ByteBuffer
(最佳选择 - 简洁且易于阅读):您还可以使用
DataOutputStream
(更详细):最后,您可以手动执行此操作(取自 Hector 代码中的
LongSerializer
)(更难阅读):然后您可以通过简单的方法将这些字节附加到现有数组中环形:
There are multiple ways to do it:
Use a
ByteBuffer
(best option - concise and easy to read):You can also use
DataOutputStream
(more verbose):Finally, you can do this manually (taken from the
LongSerializer
in Hector's code) (harder to read):Then you can append these bytes to your existing array by a simple loop:
如果你想真正深入了解......
If you want to really get under the hood...
对我来说,从时间角度来看,ByteBuffer 和其他实用程序都很昂贵。您可以使用以下两种方法:
// 第一种方法(即使用第二种方法),它返回分配并满足的数组
// 如果您已经分配了缓冲区并且想要写入 long a,则此方法很有用数组中的特定位置。
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
// this method is useful if you have already allocated the buffer and you want to write the long a specific location in the array.
看起来您无法对字节数组进行切片以将某些内容插入到子集中,而无需逐字节进行操作。查看 在 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.
我更新这篇文章是因为我刚刚宣布了一个库的预发布版本,该库将把 long 转换为字节数组(然后再转换回来)。该库非常小,可以将任何 java 原语转换为字节数组。
http://rschilling.wordpress.com/2013/ 09/26/预发布-公告-pend-oreille/
http://code.google.com/p/pend-oreille/
如果您使用它你可以做一些事情,比如将长数组转换为字节数组:
你也可以转换单个长值。
请随时提供一些反馈。
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:
You can also convert a single long value as well.
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/