<bdo id='aRiBr'></bdo><ul id='aRiBr'></ul>

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

      2. <legend id='aRiBr'><style id='aRiBr'><dir id='aRiBr'><q id='aRiBr'></q></dir></style></legend>
      3. <small id='aRiBr'></small><noframes id='aRiBr'>

        <tfoot id='aRiBr'></tfoot>

        如何将 Long 转换为 byte[] 并返回到 java

        时间:2023-07-27
            • <bdo id='PvkxW'></bdo><ul id='PvkxW'></ul>

              <legend id='PvkxW'><style id='PvkxW'><dir id='PvkxW'><q id='PvkxW'></q></dir></style></legend>

              <i id='PvkxW'><tr id='PvkxW'><dt id='PvkxW'><q id='PvkxW'><span id='PvkxW'><b id='PvkxW'><form id='PvkxW'><ins id='PvkxW'></ins><ul id='PvkxW'></ul><sub id='PvkxW'></sub></form><legend id='PvkxW'></legend><bdo id='PvkxW'><pre id='PvkxW'><center id='PvkxW'></center></pre></bdo></b><th id='PvkxW'></th></span></q></dt></tr></i><div id='PvkxW'><tfoot id='PvkxW'></tfoot><dl id='PvkxW'><fieldset id='PvkxW'></fieldset></dl></div>

            • <small id='PvkxW'></small><noframes id='PvkxW'>

                    <tbody id='PvkxW'></tbody>
                1. <tfoot id='PvkxW'></tfoot>
                  本文介绍了如何将 Long 转换为 byte[] 并返回到 java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何将 long 转换为 byte[] 并返回 Java?

                  How do I convert a long to a byte[] and back in Java?

                  我正在尝试将 long 转换为 byte[] 以便能够通过TCP 连接.另一方面,我想把那个 byte[] 转换回 double.

                  I'm trying convert a long to a byte[] so that I will be able to send the byte[] over a TCP connection. On the other side I want to take that byte[] and convert it back into a double.

                  推荐答案

                  public byte[] longToBytes(long x) {
                      ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
                      buffer.putLong(x);
                      return buffer.array();
                  }
                  
                  public long bytesToLong(byte[] bytes) {
                      ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
                      buffer.put(bytes);
                      buffer.flip();//need flip 
                      return buffer.getLong();
                  }
                  

                  或者封装在一个类中以避免重复创建ByteBuffers:

                  Or wrapped in a class to avoid repeatedly creating ByteBuffers:

                  public class ByteUtils {
                      private static ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);    
                  
                      public static byte[] longToBytes(long x) {
                          buffer.putLong(0, x);
                          return buffer.array();
                      }
                  
                      public static long bytesToLong(byte[] bytes) {
                          buffer.put(bytes, 0, bytes.length);
                          buffer.flip();//need flip 
                          return buffer.getLong();
                      }
                  }
                  

                  <小时>

                  由于它变得如此流行,我只想提一下,我认为在绝大多数情况下使用像 Guava 这样的库会更好.如果您对库有一些奇怪的反对意见,您可能应该首先考虑 this answer 对于原生 java 解决方案.我认为我的回答真正要解决的主要问题是您不必自己担心系统的字节序.


                  Since this is getting so popular, I just want to mention that I think you're better off using a library like Guava in the vast majority of cases. And if you have some strange opposition to libraries, you should probably consider this answer first for native java solutions. I think the main thing my answer really has going for it is that you don't have to worry about the endian-ness of the system yourself.

                  这篇关于如何将 Long 转换为 byte[] 并返回到 java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:“不兼容的类型:void 无法转换为 ..."是什么意 下一篇:如何在 Java 中将字符串转换为 InputStream?

                  相关文章

                  最新文章

                  1. <small id='G7vsH'></small><noframes id='G7vsH'>

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

                      <tfoot id='G7vsH'></tfoot>
                        <bdo id='G7vsH'></bdo><ul id='G7vsH'></ul>