Java int long double与byte数组互转

package com.pancm.Test;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;


public class Contants {
    public static void main(String[] args) throws IOException {
        int a = 1233123123;
        long b = 21231231;
        double cc = 3233;
        String ss = "abc";
        byte[] bytesInt = intToByteArray(a);
        byte[] bytesLong = longToByteArray(b);
        byte[] bytesDouble = doubleToByteArray(cc);

        double random = Math.round(Math.random() * 10000);
        long long1 = new Double(random).longValue();
        System.out.println(random + "=====:" + long1);

        System.out.println(intToByteArray(a).length);
        System.out.println(longToByteArray(b).length);
        System.out.println(doubleToByteArray(cc).length);
        System.out.println(stringToByteArray(ss).length);

        System.out.println("==============合并解析===========");
        byte[] bytesMerge = mergeBtyesprocess(bytesLong, bytesInt);
        System.out.println(bytesMerge.length);
        System.out.println(byteArrayToLong(subByte(bytesMerge, 0, 8)));
        System.out.println(bytesMerge.length);
        System.out.println(byteArrayToInt(subByte(bytesMerge, 8, 4)));
    }

    private static byte[] intToByteArray(int number) throws IOException {
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bao);
        dos.writeInt(number);
        return bao.toByteArray();
    }

    private static byte[] longToByteArray(long l) throws IOException {
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bao);
        dos.writeLong(l);
        return bao.toByteArray();
    }

    private static byte[] doubleToByteArray(double l) throws IOException {
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bao);
        dos.writeDouble(l);
        System.out.println(bytes2Double(bao.toByteArray()));
        return bao.toByteArray();
    }

    private static byte[] floatToByteArray(float l) throws IOException {
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bao);
        dos.writeFloat(l);
        return bao.toByteArray();
    }

    private static byte[] stringToByteArray(String l) throws IOException {
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bao);
        dos.writeBytes(l);
        return bao.toByteArray();
    }

    private static byte[] mergeBytes(byte[] data1, byte[] data2) {
        byte[] data3 = new byte[data1.length + data2.length];
        System.arraycopy(data1, 0, data3, 0, data1.length);
        System.arraycopy(data2, 0, data3, data1.length, data2.length);
        return data3;
    }

    /**
     * 截取byte数组   不改变原数组
     *
     * @param b      原数组
     * @param off    偏差值(索引)
     * @param length 长度
     * @return 截取后的数组
     */
    private static byte[] subByte(byte[] b, int off, int length) {
        byte[] b1 = new byte[length];
        System.arraycopy(b, off, b1, 0, length);
        return b1;
    }

    /**
     * 合并byte数组
     */
    private static byte[] mergeBtyesprocess(byte[] byte1, byte[] byte2) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        os.write(byte1);
        os.write(byte2);
        return os.toByteArray();
    }

    /**
     * byte[]转int
     *
     */
    public static int byteArrayToInt(byte[] bytes) {
        int value = 0;
        //由高位到低位
        for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (bytes[i] & 0x000000FF) << shift;//往高位游
        }
        return value;
    }

    public static double bytes2Double(byte[] arr) {
        long value = 0;
        for (int i = 0; i < 8; i++) {
            value |= ((long) (arr[i] & 0xff)) << (8 * i);
        }
        return Double.longBitsToDouble(value);
    }

    public static long byteArrayToLong(byte[] bytes) {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.put(bytes, 0, bytes.length);
        buffer.flip();
        return buffer.getLong();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值