在JAVA中如何把int型转换成byte型, 比如int a=135 转换成byte型,求,详细解答步骤 谢谢

2025-03-07 14:38:52
推荐回答(2个)
回答1:

public static void main(String[] args) {
  // TODO Auto-generated method stub
  int i = 65535;   
  byte[] a = new byte[4];
        a[0] = (byte) (0xff & i);
        a[1] = (byte) ((0xff00 & i) >> 8);
        a[2] = (byte) ((0xff0000 & i) >> 16);
        a[3] = (byte) ((0xff000000 & i) >> 24);
        //a  为转换后的byte
        for (int j = 0; j < a.length; j++) {
       System.out.println(a[j]);
  }
 }

同时赞成楼上说的,需要注意

回答2:

少年你知道byte的范围吗?-128-127,135超出了范围。