byte与其他基本数据类型转换

自己从百度上零零散散整的项目的工具类,不多说,直接贴代码,有问题的,望指教

 private static ByteBuffer buffer = ByteBuffer.allocate(8);

    /*
         byte字节数组转double
     */
    public static Double getDouble(byte[] temp) {
        long value = 0;
        for (int i = 0; i < temp.length; i++) {
            value |= ((long) (temp[i] & 0xff)) << (8 * i);
        }
        double d = Double.longBitsToDouble(value);
        return d;
    }

    /*
         double转byte字节数组
     */
    public static byte[] putDouble(byte[] arr, double number, int index) {
        int len = index - 1;
        int arrLen = arr.length;
        boolean b = isOutOfArrLength(arrLen, len); // 判断当前数组长度是否大于转换的数组长度
        Long l = Double.doubleToLongBits(number);
        if (b) {
            for (int i = 0; i <= 7; i++) {
                arr[i] = l.byteValue();
                l = l >> 8;
            }
        } else {
            // 如果当前数组长度小于转换的数组长度,就根据index截取转换的数组元素
            l = l >> (8 * index);
            for (int j = arrLen - index - 1; j >= 0; j--) {
                arr[j] = l.byteValue();
                l = l >> 8;
            }
        }
//      for (byte c : arr) {
//          System.out.println("c--->" + c);
//      }
        return arr;
    }

    public static boolean isOutOfArrLength(int arrLength, int index) {
        boolean b;
        if (arrLength > index) {
            b = true;
        } else {
            b = false;
        }
        return b;
    }

    /*
         16进制转二进制
     */
    public static String convertHexToBinary3(String hexString) {
        long l = Long.parseLong(hexString, 16);
        String binaryString = Long.toBinaryString(l);
        // int shouldBinaryLen = hexString.length() * 4;
        StringBuffer addZero = new StringBuffer();
        // int addZeroNum = shouldBinaryLen - binaryString.length();
        /*
         * for (int i = 1; i <= addZeroNum; i++) { addZero.append("0"); }
         */
        return addZero.toString() + binaryString;
    }

    /*
         二进制转10进制
     */
    public static int er(int s) {
        int sum = 0;
        int i = 0;
        while (s != 0) {
            sum = (int) (sum + s % 10 * (Math.pow(2, i)));
            s = s / 10;
            i++;
        }
        return sum;
    }

    /*
            10转成16进制
     */
    public static String shi(String str) {
        StringBuffer sb = new StringBuffer(str.length());
        String tmp = null;
        for (int i = 0; i < str.length(); i = i + 2) {
            tmp = str.substring(i, i + 2);
            String strtmp = Integer.toHexString(Integer.parseInt(tmp));
            sb.append(strtmp);
        }
        // System.out.println(sb.toString());
        return sb.toString();
    }

    /*
     16进制转成10进制
     */
    public static void main(String[] args) {
        long l = 12340006;
        byte[] bytes = Conversion.longToBytes(l);
        for (byte aByte : bytes) {
            System.out.println(aByte);
        }
    }

    /*
          int转byte[]
     */
    public static byte[] intToBytes(int value) {
        byte[] bytes = new byte[4];
        bytes[3] = (byte) (value >> 24);
        bytes[2] = (byte) (value >> 16);
        bytes[1] = (byte) (value >> 8);
        bytes[0] = (byte) (value >> 0);
        return bytes;
    }

    /*
         short转byte[]
     */
    public static byte[] shortToByte(short value) {
        byte[] bytes = new byte[2];
        bytes[1] = (byte) (value >> 8);
        bytes[0] = (byte) (value >> 0);
        return bytes;
    }

    /*
         byte转16进制
     */
    public static String byteToHex(byte b) {
        int i = b & 0xFF;
        return Integer.toHexString(i);
    }

    /*
         byte[]转int
     */
    public static int bytesToInt(byte[] bytes) {
        return (int) ((((bytes[3] & 0xff) << 24)
                | ((bytes[2] & 0xff) << 16)
                | ((bytes[1] & 0xff) << 8) | ((bytes[0] & 0xff) << 0)));
    }

    /*
         byte[]转short
     */
    public static short bytesToShort(byte[] bytes) {
        return (short) (((bytes[1] << 8) | bytes[0] & 0xff));
    }

    /*
        byte 数组与 long 的相互转换
     */
    public static byte[] longToBytes(long x) {
      /*  buffer.putLong(0, x);
        buffer.flip();//need flip
        return buffer.array();*/
        byte[] b = new byte[8];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Long(x & 0xff).byteValue();//
            x = x >> 8; // 向右移8位
        }
        return b;
    }

    public static long bytesToLong(byte[] bytes) {
        buffer.put(bytes, 0, bytes.length);
        buffer.flip();//need flip
        return buffer.getLong();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值