When converting from a byte to an int why do you have to do a logical AND with 0xFF.
For example:
int i = 'A' & 0xff;
First of all, in that line of code there is no conversion from a byte to an int. The 'A' is a character literal (its type is
char, not
byte). The 0xff is an integer literal. Also, the AND operator & is not a
logical AND, it is a
bitwise AND. The logical AND operator is &&.
Doing a bitwise AND with 0xff means keeping the lower 8 bits and clearing all higher bits.
About the second question: You first shift i four bits to the right and with the & operation you keep the lower 4 bits and mask the rest off.
In hexadecimal, each digit represents 4 bits in the value. Take for example the value 156 (decimal). In binary, this is: 10011100. It is easy to convert this to hexadecimal: split the binary number into groups of 4 bits: 1001 1100. Each group of 4 bits corresponds to 1 hexadecimal digit. 1001 = 9 and 1100 = C, so 156 = 10011100 = 0x9C. See this:
Hexadecimal (Wikipedia)