美文网首页
Java 1: 二进制学习

Java 1: 二进制学习

作者: 老柯南 | 来源:发表于2019-03-31 11:47 被阅读0次
    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

    相关文章

      网友评论

          本文标题:Java 1: 二进制学习

          本文链接:https://www.haomeiwen.com/subject/uwgcbqtx.html