一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

java怎么样将基本类型与字节流转化?Java基本类型转换成字节流

时间:2017-02-16 编辑:简简单单 来源:一聚教程网

简单的做了点Java 基本类型到字节的转换,测试了下,还能用... ...

这些主要是在用于网络通信的时候,对方如果传递给你一个字节流,里面包含的是基本类型的字节流,可以通过这个来转换成java 基本类型。

 

 代码如下复制代码

/**

     * 
     * double转换byte
     * 
     * @param arr
     *            byte[]
     * 
     * @param param
     *            double double类型的参数
     * 
     * @param index
     *            int
     * 
     */

    public static void putDouble(byte[] arr, double param, int index) {

        int len = (index - 1) + 8;

        int arrLen = arr.length;

        boolean b = isOutOfArrLength(arrLen, len); // 判断当前数组长度是否大于转换的数组长度

        Long l = Double.doubleToLongBits(param);

        if (b) {

            for (int i = 7; i >= 0; i--) {

                arr[index + i] = l.byteValue();

                l = l >> 8;

            }

        } else {

            // 如果当前数组长度小于转换的数组长度,就根据index截取转换的数组元素

            l = l >> (8 * index);

            for (int j = arrLen - index - 1; j >= 0; j--) {

                arr[index + j] = l.byteValue();

                l = l >> 8;

            }

        }

    }

    /**
     * 
     * float转换byte
     * 
     * 
     * 
     * @param arr
     *            byte[]
     * 
     * @param param
     *            float float类型的参数
     * 
     * @param index
     *            int
     * 
     */

    public static void putFloat(byte[] arr, float param, int index) {

        int len = (index - 1) + 4;

        int arrLen = arr.length;

        boolean b = isOutOfArrLength(arrLen, len); // 判断当前数组长度是否大于转换的数组长度

        int l = Float.floatToIntBits(param);

        if (b) {

            for (int i = 3; i >= 0; i--) {

                arr[index + i] = new Integer(l).byteValue();

                l = l >> 8;

            }

        } else {

            // 如果当前数组长度小于转换的数组长度,就根据index截取转换的数组元素

            l = l >> (8 * index);

            for (int j = arrLen - index - 1; j >= 0; j--) {

                arr[index + j] = new Integer(l).byteValue();

                l = l >> 8;

            }

        }

    }

    /**
     * 
     * 字符到字节转换
     * 
     * 
     * 
     * @param arr
     *            byte[]
     * 
     * @param ch
     *            char char类型的参数
     * 
     * @param index
     *            int
     * 
     * @return
     * 
     */

    public static void putChar(byte[] arr, char ch, int index) {

        int len = (index - 1) + 4;

        boolean b = isOutOfArrLength(arr.length, len); // 判断当前数组长度是否大于转换的数组长度

        if (b) {

            int temp = (int) ch;

            for (int i = 1; i >= 0; i--) {

                arr[index + i] = new Integer(temp & 0xff).byteValue();

                temp = temp >> 8;

            }

        }

    }

    /**
     * 
     * 转换long型为byte数组
     * 
     * 
     * 
     * @param arr
     *            byte[]
     * 
     * @param param
     *            long
     * 
     * @param index
     *            int
     * 
     */

    public static void putLong(byte[] arr, long param, int index) {

        int len = (index - 1) + 8;

        int arrLen = arr.length;

        boolean b = isOutOfArrLength(arrLen, len); // 判断当前数组长度是否大于转换的数组长度

        if (b) {

            arr[index + 0] = (byte) ((param >> 56) & 0xff);

            arr[index + 1] = (byte) ((param >> 48) & 0xff);

            arr[index + 2] = (byte) ((param >> 40) & 0xff);

            arr[index + 3] = (byte) ((param >> 32) & 0xff);

            arr[index + 4] = (byte) ((param >> 24) & 0xff);

            arr[index + 5] = (byte) ((param >> 16) & 0xff);

            arr[index + 6] = (byte) ((param >> 8) & 0xff);

            arr[index + 7] = (byte) (param & 0xff);

        } else {

            // 如果当前数组长度小于转换的数组长度,就根据index截取转换的数组元素

            param = param >> (8 * index);

            for (int i = arrLen - index - 1; i >= 0; i--) {

                arr[index + i] = (byte) (param & 0xff);

                param = param >> 8;

            }

        }

    }

    /**
     * 
     * int类型转换成byte数组
     * 
     * 
     * 
     * @param arr
     *            byte[]
     * 
     * @param param
     *            int int类型的参数
     * 
     * @param index
     *            int
     * 
     */

    public static void putInt(byte[] arr, int param, int index) {

        int len = (index - 1) + 4;

        boolean b = isOutOfArrLength(arr.length, len); // 判断当前数组长度是否大于转换的数组长度

        if (b) {

            arr[index + 0] = (byte) ((param >> 24) & 0xff);

            arr[index + 1] = (byte) ((param >> 16) & 0xff);

            arr[index + 2] = (byte) ((param >> 8) & 0xff);

            arr[index + 3] = (byte) (param & 0xff);

        }

    }

    /**
     * 
     * short类型转换成byte数组
     * 
     * 
     * 
     * @param arr
     *            byte[]
     * 
     * @param param
     *            short
     * 
     * @param index
     *            int
     * 
     */

    public static void putShort(byte[] arr, short param, int index) {

        int len = (index - 1) + 2;

        boolean b = isOutOfArrLength(arr.length, len); // 判断当前数组长度是否大于转换的数组长度

        if (b) {

            arr[index + 0] = (byte) ((param >> 8) & 0xff);

            arr[index + 1] = (byte) (param & 0xff);

        }

    }

    /**
     * 
     * 字符串转换成byte数组
     * 
     * 
     * 
     * @param arr
     *            byte[]
     * 
     * @param str
     *            String
     * 
     * @param index
     *            int
     * 
     * @throws java.io.UnsupportedEncodingException
     * 
     */

    public static void putString(byte[] arr, String str, int index) {

        try {

            byte[] bb = str.getBytes("GBK");

            int len = index + bb.length;

            boolean b = isOutOfArrLength(arr.length, len);

            if (b) {

                for (int i = 0; i < bb.length; i++) {

                    arr[index + i] = bb[i];

                }

            } else {

                // 如果当前数组长度小于转换的数组长度,就根据index截取转换的数组元素

                for (int j = 0; j < arr.length - index; j++) {

                    arr[index + j] = bb[j];

                }

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    /**
     * 
     * 判断数组下标是否越界
     * 
     * 
     * 
     * @param arrLength
     * 
     * 数组总长度
     * 
     * @param index
     * 
     * 数组偏移量
     * 
     * @return
     * 
     */

    public static boolean isOutOfArrLength(int arrLength, int index) {

        boolean b;

        if (arrLength > index) {

            b = true;

        } else {

            b = false;

        }

        return b;

    }

    // long类型转成byte数组
    public static byte[] longToBytes(long number) {
        long temp = number;
        byte[] b = new byte[8]; //long型是8个字节.
        for (int i = 0; i < b.length; i++) {
            b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位
            temp = temp >> 8; // 向右移8位
        }
        return b;
    }

    // byte数组转成long
    public static long bytesToLong(byte[] b) {
        long s = 0;
        long s0 = b[0] & 0xff;// 最低位
        long s1 = b[1] & 0xff;
        long s2 = b[2] & 0xff;
        long s3 = b[3] & 0xff;
        long s4 = b[4] & 0xff;// 最低位
        long s5 = b[5] & 0xff;
        long s6 = b[6] & 0xff;
        long s7 = b[7] & 0xff;
        // s0不变
        s1 <<= 8;
        s2 <<= 16;
        s3 <<= 24;
        s4 <<= 8 * 4;
        s5 <<= 8 * 5;
        s6 <<= 8 * 6;
        s7 <<= 8 * 7;
        s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
        return s;
    }

    //int 转换为byte
    public static byte[] intToBytes(int number) {

        int temp = number;
        byte[] b = new byte[4];

        for (int i = 0; i < b.length; i++) {
            b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
            temp = temp >> 8; // 向右移8位
        }
        return b;
    }

    //字节转int
    public static int bytesToInt(byte[] b) {
        int s = 0;
        int s0 = b[0] & 0xff;// 最低位
        int s1 = b[1] & 0xff;
        int s2 = b[2] & 0xff;
        int s3 = b[3] & 0xff;
        s3 <<= 24;
        s2 <<= 16;
        s1 <<= 8;
        s = s0 | s1 | s2 | s3;
        return s;
    }

    // 浮点到字节转换
    public static byte[] doubleToBytes(double d) {
        byte writeBuffer[] = new byte[8];
        long v = Double.doubleToLongBits(d);
        writeBuffer[0] = (byte) (v >>> 56);
        writeBuffer[1] = (byte) (v >>> 48);
        writeBuffer[2] = (byte) (v >>> 40);
        writeBuffer[3] = (byte) (v >>> 32);
        writeBuffer[4] = (byte) (v >>> 24);
        writeBuffer[5] = (byte) (v >>> 16);
        writeBuffer[6] = (byte) (v >>> 8);
        writeBuffer[7] = (byte) (v >>> 0);
        return writeBuffer;

    }

    // 字节到浮点转换
    public static double bytesToDouble(byte[] readBuffer) {
        return Double
                .longBitsToDouble((((long) readBuffer[0] << 56)
                        + ((long) (readBuffer[1] & 255) << 48)
                        + ((long) (readBuffer[2] & 255) << 40)
                        + ((long) (readBuffer[3] & 255) << 32)
                        + ((long) (readBuffer[4] & 255) << 24)
                        + ((readBuffer[5] & 255) << 16)
                        + ((readBuffer[6] & 255) << 8) + ((readBuffer[7] & 255) << 0)));

    }

 

更多的转换,可以通过实践去获得。

热门栏目