<legend id='gXbTC'><style id='gXbTC'><dir id='gXbTC'><q id='gXbTC'></q></dir></style></legend>
      1. <tfoot id='gXbTC'></tfoot>
        • <bdo id='gXbTC'></bdo><ul id='gXbTC'></ul>
        <i id='gXbTC'><tr id='gXbTC'><dt id='gXbTC'><q id='gXbTC'><span id='gXbTC'><b id='gXbTC'><form id='gXbTC'><ins id='gXbTC'></ins><ul id='gXbTC'></ul><sub id='gXbTC'></sub></form><legend id='gXbTC'></legend><bdo id='gXbTC'><pre id='gXbTC'><center id='gXbTC'></center></pre></bdo></b><th id='gXbTC'></th></span></q></dt></tr></i><div id='gXbTC'><tfoot id='gXbTC'></tfoot><dl id='gXbTC'><fieldset id='gXbTC'></fieldset></dl></div>

        <small id='gXbTC'></small><noframes id='gXbTC'>

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

        时间:2023-07-27
      2. <legend id='NR1i2'><style id='NR1i2'><dir id='NR1i2'><q id='NR1i2'></q></dir></style></legend>

          <i id='NR1i2'><tr id='NR1i2'><dt id='NR1i2'><q id='NR1i2'><span id='NR1i2'><b id='NR1i2'><form id='NR1i2'><ins id='NR1i2'></ins><ul id='NR1i2'></ul><sub id='NR1i2'></sub></form><legend id='NR1i2'></legend><bdo id='NR1i2'><pre id='NR1i2'><center id='NR1i2'></center></pre></bdo></b><th id='NR1i2'></th></span></q></dt></tr></i><div id='NR1i2'><tfoot id='NR1i2'></tfoot><dl id='NR1i2'><fieldset id='NR1i2'></fieldset></dl></div>
            1. <tfoot id='NR1i2'></tfoot>
              • <small id='NR1i2'></small><noframes id='NR1i2'>

                  <tbody id='NR1i2'></tbody>

                  <bdo id='NR1i2'></bdo><ul id='NR1i2'></ul>
                  本文介绍了将 long 转换为字节数组并将其添加到另一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想更改字节数组中的值以将长时间戳值放入 M​​SB.有人可以告诉我最好的方法是什么.我不想逐位插入值,我认为这是非常低效的.

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

                  我想要的是这样的:

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

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

                  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?


                  System.currentTimeMillis() 似乎比Calendar.getTimeInMillis() 快,所以将上面的代码替换为它.如有错误请指正.


                  It seems that System.currentTimeMillis() is faster than Calendar.getTimeInMillis(), so replacing the above code by it.Please correct me if wrong.

                  推荐答案

                  有多种方法:

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

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

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

                  这篇关于将 long 转换为字节数组并将其添加到另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Java char 也是 int 吗? 下一篇:double a = a + int b 和 int a += double b 有什么区别?

                  相关文章

                  最新文章

                    1. <legend id='vpVxD'><style id='vpVxD'><dir id='vpVxD'><q id='vpVxD'></q></dir></style></legend>
                      <i id='vpVxD'><tr id='vpVxD'><dt id='vpVxD'><q id='vpVxD'><span id='vpVxD'><b id='vpVxD'><form id='vpVxD'><ins id='vpVxD'></ins><ul id='vpVxD'></ul><sub id='vpVxD'></sub></form><legend id='vpVxD'></legend><bdo id='vpVxD'><pre id='vpVxD'><center id='vpVxD'></center></pre></bdo></b><th id='vpVxD'></th></span></q></dt></tr></i><div id='vpVxD'><tfoot id='vpVxD'></tfoot><dl id='vpVxD'><fieldset id='vpVxD'></fieldset></dl></div>
                      • <bdo id='vpVxD'></bdo><ul id='vpVxD'></ul>
                    2. <tfoot id='vpVxD'></tfoot>
                    3. <small id='vpVxD'></small><noframes id='vpVxD'>