Java byte转换为int

Java byte到Int的转换
Java byte到Int的转换有两种:

  1. 带符号转换,数值转换
  2. 无符号转换

byte本身是带正负符号的, 默认向上转换也是带符号

带符号转换

byte本身是带正负符号的, 默认向上转换也是带符号

方法1 默认转换

		byte b = -3;
		int i = b;
		System.out.println(i); // 结果是 -3

方法2 强制转换

		byte b = -3;
		int i = (int)b;
		System.out.println(i); // 结果是 -3

无符号转换

方法1 i = Byte.toUnsignedInt(b);

i = Byte.toUnsignedInt(b);

源码是👇

    /**
     * Converts the argument to an {@code int} by an unsigned
     * conversion.  In an unsigned conversion to an {@code int}, the
     * high-order 24 bits of the {@code int} are zero and the
     * low-order 8 bits are equal to the bits of the {@code byte} argument.
     *
     * Consequently, zero and positive {@code byte} values are mapped
     * to a numerically equal {@code int} value and negative {@code
     * byte} values are mapped to an {@code int} value equal to the
     * input plus 2<sup>8</sup>.
     *
     * @param  x the value to convert to an unsigned {@code int}
     * @return the argument converted to {@code int} by an unsigned
     *         conversion
     * @since 1.8
     */
    public static int toUnsignedInt(byte x) {
        return ((int) x) & 0xff;
    }

1.8版才有? 😓

方法2 i = b&0xff;

i = b&0xff;
或者
i = b&0xFF;

方法3 i = b&255;

i = b&255;

方法4 if(b<0)i=b+256; else i=b;

if(b<0)i=b+256; else i=b;

方法5 i = b<0 ? b+256 : b;

i = b<0 ? b+256 : b;

方法6 if(b>=0)i=b; else i=b+256; //是大于等于 , 而不能是大于

if(b>=0)i=b; else i=b+256;  //是大于等于 , 而不能是大于

方法7 i = b>=0 ? b : b+256; //是大于等于 , 而不能是大于

i = b>=0 ? b : b+256;        //是大于等于 , 而不能是大于

测试代码1

public class T2206040611 {

	public static void main(String[] args) {
		byte b = -3;
		int i;
		i = b;		System.out.println(i);
		i = (int)b;		System.out.println(i);
		i = Byte.toUnsignedInt(b);		System.out.println(i);
		i = b&0xff;		System.out.println(i);
		i = b&255;		System.out.println(i);
		if(b<0)i=b+256; else i=b; System.out.println(i);
		i = b<0 ? b+256 : b; System.out.println(i);
	}

}

结果

-3
-3
253
253
253
253
253

测试代码230601

package p230601;

public class Byteint的方法230601 {
    public static void main(String[] args) {
        int i;
        for(byte b : new byte[]{ -128, -64, -32, -16, -8, -4, -3, -2, -1, 0, 1, 2, 3, 4, 8, 16, 32, 64, 127 }){

            i = b;		p(i);
            i = (int)b;		p(i);
            i = Byte.toUnsignedInt(b);		p(i);
            i = b&0xff;		p(i);
            i = b&255;		p(i);

            if(b<0)i=b+256; else i=b; p(i);  //是小于< , 而不能是小于等于
            i = b<0 ? b+256 : b; p(i);       //是小于< , 而不能是小于等于

            if(b>=0)i=b; else i=b+256; p(i);  //是大于等于 , 而不能是大于
            i = b>=0 ? b : b+256; p(i);       //是大于等于 , 而不能是大于


            System.out.println();
        }
    }
    
    static void p(int i){
        if(i==-128)System.out.print(""+i+"\t\t");
        else System.out.print(""+i+"\t\t\t");
    }
}

结果

-128		-128		128			128			128			128			128			128			128			
-64			-64			192			192			192			192			192			192			192			
-32			-32			224			224			224			224			224			224			224			
-16			-16			240			240			240			240			240			240			240			
-8			-8			248			248			248			248			248			248			248			
-4			-4			252			252			252			252			252			252			252			
-3			-3			253			253			253			253			253			253			253			
-2			-2			254			254			254			254			254			254			254			
-1			-1			255			255			255			255			255			255			255			
0			0			0			0			0			0			0			0			0			
1			1			1			1			1			1			1			1			1			
2			2			2			2			2			2			2			2			2			
3			3			3			3			3			3			3			3			3			
4			4			4			4			4			4			4			4			4			
8			8			8			8			8			8			8			8			8			
16			16			16			16			16			16			16			16			16			
32			32			32			32			32			32			32			32			32			
64			64			64			64			64			64			64			64			64			
127			127			127			127			127			127			127			127			127			
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kfepiza

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值