Converting from byte to int in java

后端 未结 6 835
长发绾君心
长发绾君心 2020-12-02 09:53

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


        
相关标签:
6条回答
  • 2020-12-02 10:25

    Primitive data types (such as byte) don't have methods in java, but you can directly do:

    int i=rno[0];
    
    0 讨论(0)
  • 2020-12-02 10:26

    I thought it would be:

    byte b = (byte)255;
    int i = b &255;
    
    0 讨论(0)
  • 2020-12-02 10:32
    byte b = (byte)0xC8;
    int v1 = b;       // v1 is -56 (0xFFFFFFC8)
    int v2 = b & 0xFF // v2 is 200 (0x000000C8)
    

    Most of the time v2 is the way you really need.

    0 讨论(0)
  • 2020-12-02 10:40

    Bytes are transparently converted to ints.

    Just say

    int i= rno[0];
    
    0 讨论(0)
  • 2020-12-02 10:41

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

    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.

    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
    

    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
    

    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
    

    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.


    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.

    0 讨论(0)
  • 2020-12-02 10:42

    if you want to combine the 4 bytes into a single int you need to do

    int i= (rno[0]<<24)&0xff000000|
           (rno[1]<<16)&0x00ff0000|
           (rno[2]<< 8)&0x0000ff00|
           (rno[3]<< 0)&0x000000ff;
    

    I use 3 special operators | is the bitwise logical OR & is the logical AND and << is the left shift

    in essence I combine the 4 8-bit bytes into a single 32 bit int by shifting the bytes in place and ORing them together

    I also ensure any sign promotion won't affect the result with & 0xff

    0 讨论(0)
提交回复
热议问题