学无先后,达者为师

网站首页 java综合 正文

Java基础之short型数字与字节数组互转

作者:左耳听过流年的声 更新时间: 2022-01-23 java综合

public class ByteBufferDemo {
    public static void main(String[] args) {
        System.out.print(getInt((short) -23432) + "  ");
    }

    public static int getInt(short tmp) {//大端模式存放,高位放低字节,低位放高字节

        short number = tmp;
        byte[] temp = new byte[2];
        temp[0] = (byte) (number >> 8);
        temp[1] = (byte) (number);  //short转字节数组


        short temp0 = temp[0];
        short temp1 = temp[1];
        short tempRes = (short) (temp0 << 8);
        short result = (short) (tempRes + (temp1 > 0 ? temp1 : temp1 & 0x00ff));//字节数组转short
        return result;
    }

}

原文链接:https://blog.csdn.net/weixin_42414567/article/details/122411097

栏目分类
最近更新