Java 1: 二进制学习

image.png
image.png
image.png
public class EncodeUtils {

    /**
     * int 4个字节 32bite
     * 小端法
     *
     * @param intPara
     * @return
     */
    public static byte[] int2Bytes(int intPara) {

        byte[] bytes = new byte[4];

        bytes[0] = (byte) ((int) (intPara >> 0 * 8) & 0xff);
        bytes[1] = (byte) ((int) (intPara >> 1 * 8) & 0xff);
        bytes[2] = (byte) ((int) (intPara >> 2 * 8) & 0xff);
        bytes[3] = (byte) ((int) (intPara >> 3 * 8) & 0xff);

        return bytes;
    }

    public static int bytes2Int(byte[] intbytes) {

        if (intbytes == null || intbytes.length != 4) {
            throw new IllegalArgumentException("参数错误");
        }
        int int0 = (intbytes[0] & 0xff) << 0 * 8;
        int int1 = (intbytes[1] & 0xff) << 1 * 8;
        int int2 = (intbytes[2] & 0xff) << 2 * 8;
        int int3 = (intbytes[3] & 0xff) << 3 * 8;

        return int0 + int1 + int2 + int3;

    }
}
image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容