How are integers cast to bytes in Java?
This is called a narrowing primitive conversion. According to the spec:
A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.
So it's the second option you listed (directly copying the last 8 bits).
I am unsure from your question whether or not you are aware of how signed integral values are represented, so just to be safe I'll point out that the byte value 1111 1111 is equal to -1 in the two's complement system (which Java uses).
Converting from byte to int in Java
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.
Explicit conversion from int to byte in Java
Logic is simple, Java numbers are always signed in two's complement.
Now a byte has 8 bits and 128 is 10000000
. When you do
int i = 128
you end up with:
i == 00000000 00000000 00000000 10000000
When you cast it to a byte you the 24 most significative are truncated, so you end up with
b == 10000000
but a Java byte
is signed, and 128 can't be represented, since it overflows and wraps around. So what happens is that the value ends up as 128 - 256 = -128
(that's because of two's complement).
How to Convert Int to Unsigned Byte and Back
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
How to convert int to byte[]?
You forgot to flip()
your buffer after putting data in.
After you've put the int
in the buffer, the position is at the end of the buffer. Trying to read data results in BufferUnderflowException
(not overflow) since there are no bytes left to read in the buffer.
Why cast an int to byte in BufferedOutputStream write method that takes an int argument?
By casting the intByte
to a byte
, the value is narrowed from 32-bit to 8-bit. The write()
method can accept a byte even though its signature asks for an int -- this is called widening (no cast required). That part is inconsequential. But the cast from int
to byte
is important since it could dramatically affect the value of the variable by trimming off the leading 24 bits. Try this, for example, and see what happens:
int i = 13465878;
byte b = (byte)i;
System.out.println("i as a byte is " + b);
Convert integer into byte array (Java)
Have a look at the ByteBuffer class.
ByteBuffer b = ByteBuffer.allocate(4);
//b.order(ByteOrder.BIG_ENDIAN); // optional, the initial order of a byte buffer is always BIG_ENDIAN.
b.putInt(0xAABBCCDD);
byte[] result = b.array();
Setting the byte order ensures that result[0] == 0xAA
, result[1] == 0xBB
, result[2] == 0xCC
and result[3] == 0xDD
.
Or alternatively, you could do it manually:
byte[] toBytes(int i)
{
byte[] result = new byte[4];
result[0] = (byte) (i >> 24);
result[1] = (byte) (i >> 16);
result[2] = (byte) (i >> 8);
result[3] = (byte) (i /*>> 0*/);
return result;
}
The ByteBuffer
class was designed for such dirty hands tasks though. In fact the private java.nio.Bits
defines these helper methods that are used by ByteBuffer.putInt()
:
private static byte int3(int x) { return (byte)(x >> 24); }
private static byte int2(int x) { return (byte)(x >> 16); }
private static byte int1(int x) { return (byte)(x >> 8); }
private static byte int0(int x) { return (byte)(x >> 0); }
Related Topics
Raising a Number to a Power in Java
What Is the Priority of Casting in Java
Java.Lang.Noclassdeffounderror: Org/Hamcrest/Selfdescribing
In Which Case Do You Use the JPA @Jointable Annotation
How to Monitor Java Memory Usage
How Is Length Implemented in Java Arrays
Is There a MACro Recorder for Eclipse
Java Swing; Two Classes, Where to Put If Statements and New Actionlisteners
How to Change the Arrow Style in a Jcombobox
Javafx Using Objects from Maincontroller or Other Controllers in Proper Controller Class
Tomcat 8 Is Not Able to Handle Get Request with '|' in Query Parameters
How to Read Xml Response from a Url in Java
How to Add an Extra Source Directory for Maven to Compile and Include in the Build Jar