Convert Int (last 8 bits) to Byte
I need to convert a int to a byte and I am having a really really hard time. I tried shifting operators, casting all kinds of weird stuff I found on the internet. But nothing works. Everytime my Integer gets to 128 or above and I cast it to a byte it will turn negative.
My testing code:
int i1 = 234;
byte attempt1 = (byte) i1;
byte attempt2 = (byte) (i1 >> 8);
byte attempt3 = (byte) ((i1 << 24) >> 24);
System.out.println("i1: " + i1 + " | " + Integer.toBinaryString(i1));
System.out.println("attempt1: " + attempt1);
System.out.println("attempt2: " + attempt2);
System.out.println("attempt3: " + attempt3);
Please please can somebody help me? I am pretty desperated.
i1: 234 | 11101010
Thnx a lot in advance.