How to Convert Int to Unsigned Byte and Back

前端 未结 10 661
死守一世寂寞
死守一世寂寞 2020-11-28 23:04

I need to convert a number into an unsigned byte. The number is always less than or equal to 255, and so it will fit in one byte.

I also need to convert that byte ba

10条回答
  •  暖寄归人
    2020-11-28 23:47

    in java 7

    public class Main {
        public static void main(String[] args) {
            byte b =  -2;
            int i = 0 ;
            i = ( b & 0b1111_1111 ) ;
            System.err.println(i);
        }
    }
    

    result : 254

提交回复
热议问题