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

    2. <tfoot id='ehDc3'></tfoot>

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

        如何将 Int 转换为无符号字节并返回

        时间:2023-10-01

        • <legend id='MPMdF'><style id='MPMdF'><dir id='MPMdF'><q id='MPMdF'></q></dir></style></legend>
            <tbody id='MPMdF'></tbody>

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

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

                  本文介绍了如何将 Int 转换为无符号字节并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要将数字转换为无符号字节.该数字始终小于或等于 255,因此它可以容纳在一个字节中.

                  I need to convert a number into an unsigned byte. The number is always less than or equal to 255, and so it will fit in one byte.

                  我还需要将该字节转换回该数字.我将如何在 Java 中做到这一点?我已经尝试了几种方法,但都没有奏效.这是我现在要做的:

                  I also need to convert that byte back into that number. How would I do that in Java? I've tried several ways and none work. Here's what I'm trying to do now:

                  int size = 5;
                  // Convert size int to binary
                  String sizeStr = Integer.toString(size);
                  byte binaryByte = Byte.valueOf(sizeStr);
                  

                  现在将该字节转换回数字:

                  and now to convert that byte back into the number:

                  Byte test = new Byte(binaryByte);
                  int msgSize = test.intValue();
                  

                  显然,这不起作用.由于某种原因,它总是将数字转换为 65.有什么建议吗?

                  Clearly, this does not work. For some reason, it always converts the number into 65. Any suggestions?

                  推荐答案

                  一个字节总是用 Java 签名的.不过,您可以通过将其与 0xFF 进行二进制与运算来获得其无符号值:

                  A byte is always signed in Java. You may get its unsigned value by binary-anding it with 0xFF, though:

                  int i = 234;
                  byte b = (byte) i;
                  System.out.println(b); // -22
                  int i2 = b & 0xFF;
                  System.out.println(i2); // 234
                  

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

                  上一篇:如何在 Java 中实现无穷大? 下一篇:Integer vs int:关于内存

                  相关文章

                  最新文章

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

                  <tfoot id='IP7ly'></tfoot>
                    • <bdo id='IP7ly'></bdo><ul id='IP7ly'></ul>

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

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