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

    1. <tfoot id='BeiC1'></tfoot>
    2. <small id='BeiC1'></small><noframes id='BeiC1'>

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

    3. 在 Java 中从字节转换为 int

      时间:2023-07-27
      <tfoot id='PplbJ'></tfoot>

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

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

                <bdo id='PplbJ'></bdo><ul id='PplbJ'></ul>
                  <tbody id='PplbJ'></tbody>
              • 本文介绍了在 Java 中从字节转换为 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我生成了一个安全随机数,并将其值放入一个字节中.这是我的代码.

                I have generated a secure random number, and put its value into a byte. Here is my code.

                SecureRandom ranGen = new SecureRandom();
                byte[] rno = new byte[4]; 
                ranGen.nextBytes(rno);
                int i = rno[0].intValue();
                

                但我收到一个错误:

                 byte cannot be dereferenced
                

                推荐答案

                您的数组是 byte 原语,但您正试图调用它们的方法.

                Your array is of byte primitives, but you're trying to call a method on them.

                你不需要做任何明确的事情来将 byte 转换为 int,只需:

                You don't need to do anything explicit to convert a byte to an int, just:

                int i=rno[0];
                

                ...因为它不是一个沮丧的人.

                ...since it's not a downcast.

                注意 byteint 转换的默认行为是保留值的符号(记住 byte 是有符号的输入Java).比如:

                Note that the default behavior of byte-to-int conversion is to preserve the sign of the value (remember byte is a signed type in Java). So for instance:

                byte b1 = -100;
                int i1 = b1;
                System.out.println(i1); // -100
                

                如果您将 byte 视为无符号 (156) 而不是有符号 (-100),那么从 Java 8 开始就有 Byte.toUnsignedInt:

                If you were thinking of the byte as unsigned (156) rather than signed (-100), as of Java 8 there's Byte.toUnsignedInt:

                byte b2 = -100; // Or `= (byte)156;`
                int = Byte.toUnsignedInt(b2);
                System.out.println(i2); // 156
                

                在 Java 8 之前,要在 int 中获得等效值,您需要屏蔽符号位:

                Prior to Java 8, to get the equivalent value in the int you'd need to mask off the sign bits:

                byte b2 = -100; // Or `= (byte)156;`
                int i2 = (b2 & 0xFF);
                System.out.println(i2); // 156
                

                <小时>

                仅出于完整性考虑 #1:如果您确实出于某种原因想要使用 Byte 的各种方法(这里不需要strong>),您可以使用 拳击转换:


                Just for completeness #1: If you did want to use the various methods of Byte for some reason (you don't need to here), you could use a boxing conversion:

                Byte b = rno[0]; // Boxing conversion converts `byte` to `Byte`
                int i = b.intValue();
                

                字节构造函数:

                Or the Byte constructor:

                Byte b = new Byte(rno[0]);
                int i = b.intValue();
                

                但同样,你在这里不需要那个.

                But again, you don't need that here.

                仅出于完整性考虑 #2:如果它向下转换(例如,如果您试图将 int 转换为 byte),你只需要一个演员表:

                Just for completeness #2: If it were a downcast (e.g., if you were trying to convert an int to a byte), all you need is a cast:

                int i;
                byte b;
                
                i = 5;
                b = (byte)i;
                

                这向编译器保证您知道这是一个向下转换,因此您不会收到可能丢失精度"错误.

                This assures the compiler that you know it's a downcast, so you don't get the "Possible loss of precision" error.

                这篇关于在 Java 中从字节转换为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                      <tbody id='x0x4P'></tbody>

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

                      • <bdo id='x0x4P'></bdo><ul id='x0x4P'></ul>

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