Java combines integer values ​​as bytes

I have the following 4 integer values ​​that represent ARGB:

int value1 = 0xFF; int value2 = 68; int value3 = 68; int value4 = 68; 

I would like to combine the values ​​so that they represent the following:

 int test = 0xFF686868; 

My current approach is to use:

 int test2 = 0xFF | value1 | value2 | value3; 

But using this approach, the integer values ​​of test1 and test2 do not match, what am I doing wrong? I am limited to J2ME.

+4
source share
1 answer

You are almost ready: all you have to do is move the individual bytes to a position before OR , combining them.

 int test2 = (value1 << 24) | (value2 << 16) | (value3 << 8) | value4; 

Do not forget to make your 68 hex for the desired output 0xFF686868

 int value2 = 0x68; // Add 0x to all three of the 68s 
+10
source

All Articles