java中数值转换的总结

在与硬件交互时,需要将这种数值转成字节数据,按照某种协议进行传递,其中很重要的是数值和byte之间的转化。

public final class ModbusUtil {

    /**
     * 字节数组转十六进制字符串
     *
     * @param data
     * @return
     */
    public static final String valueToHex(byte[] data) {
        if (data == null) {
            return "";
        }
        return valueToHex(data, 0, data.length);
    }

    /**
     * 截取主要信息(包含子功能码),不需要获取全部
     *
     * @param data
     * @return
     */
    public static final String toSumaryHex(byte[] data) {
        if (Utils.ifNeedCompLog()) {
            if (data == null) {
                return "";
            }
            return valueToHex(data, 0, data.length);
        }

        if (data == null) {
            return "";
        }
        if (data.length < 8) {
            return "data is not right";
        }
        String funcCode = ModbusUtil.valueToHex(new byte[]{data[7]});
        switch (funcCode) {
            case "41":
            case "83":
            case "86":
            case "c1":
                return valueToHex(data, 7, 9);
            default:
                return valueToHex(data, 7, 8);
        }
    }

    /**
     * 字节数组转十六进制字符串
     *
     * @param data
     * @param off
     * @param length
     * @return
     */
    public static final String valueToHex(byte[] data, int off, int length) {
        StringBuffer stringBuffer = new StringBuffer(data.length * 2);
        for (int i = off; i < length; i++) {
            if (((int) data[i] & 0xff) < 0x10) {
                stringBuffer.append("0");
            }
            stringBuffer.append(Long.toString((int) data[i] & 0xff, 16));
            if (i < data.length - 1) {
                stringBuffer.append(" ");
            }
        }
        return stringBuffer.toString();
    }

    public static final int hexStrToInt(String hexStr) {
        if (TextUtils.isEmpty(hexStr)) {
            return -1;
        }
        String result = hexStr.substring(2);
        return Integer.parseInt(result, 16);
    }


    /**
     * 整型转换十六进制数组
     *
     * @param i
     * @return
     */
    public static final byte[] valueToHex(int i) {
        StringBuffer stringBuffer = new StringBuffer(2);
        if ((i & 0xff) < 0x10) {
            stringBuffer.append("0");
        }
        stringBuffer.append(Long.toString(i & 0xff, 16).toUpperCase());
        return stringBuffer.toString().getBytes(Charset.defaultCharset());
    }


    /**
     * 字节数组转无符号Short
     *
     * @param bytes
     * @return
     */
    public static final int regToUnsignedShort(byte[] bytes) {
        if (bytes == null) {
            return -1;
        }
        return ((bytes[0] & 0xff) << 8 | (bytes[1] & 0xff));
    }

    public static final int regToUnsignedShort(byte one, byte two) {
        return ((one & 0xff) << 8 | (two & 0xff));
    }


    /**
     * 无符号short转字节数组
     *
     * @param v
     * @return
     */
    public static final byte[] unsignedShortToReg(int v) {
        byte[] regs = new byte[2];
        regs[0] = (byte) (0xff & (v >> 8));
        regs[1] = (byte) (0xff & v);
        return regs;
    }

    /**
     * 字节数组转short
     *
     * @param bytes
     * @return
     */
    public static final short regToShort(byte[] bytes) {
        if (bytes == null) {
            return 0;
        }
        return (short) ((bytes[0] << 8) | (bytes[1] & 0xff));
    }

    /**
     * 字节数组转short
     *
     * @param bytes
     * @return
     */
    public static final short regToShort(byte[] bytes, int idx) {
        return (short) ((bytes[idx] << 8) | (bytes[idx + 1] & 0xff));
    }

    /**
     * short转字节数组
     *
     * @param s
     * @return
     */
    public static final byte[] shortToReg(short s) {
        byte[] regs = new byte[2];
        regs[0] = (byte) (0xff & (s >> 8));
        regs[1] = (byte) (0xff & s);
        return regs;
    }

    /**
     * 字节数组转int
     *
     * @param bytes
     * @return
     */
    public static final int regToInt(byte[] bytes) {
        if (bytes == null || bytes.length != 4) {
            return 0;
        }
        return (((bytes[0] & 0xff) << 24) | ((bytes[1] & 0xff) << 16)
                | ((bytes[2] & 0xff) << 8) | (bytes[3] & 0xff));
    }

    /**
     * 字节数组低位转int
     *
     * @param bytes
     * @return
     */
    public static final int regToIntByLittle(byte[] bytes) {
        return ((bytes[0] & 0xff) | ((bytes[1] & 0xff) << 8)
                | ((bytes[2] & 0xff) << 16) | (bytes[3] & 0xff) << 24);
    }

    /**
     * 小端字节数组解析
     */
    public static short bytesToShortByLittle(byte[] bytes, int idx) {
        if (bytes == null) {
            return 0;
        }
        return (short) (((bytes[idx + 1] & 0xff) << 8) | (bytes[idx] & 0xff));
    }

    /**
     * 将byte转成无符号的值对应的int
     *
     * @param value
     * @return
     */
    public static final int byteToUnsignedInt(byte value) {
        return (int) (value & 0xFF);
    }


    /**
     * int转字节数组
     *
     * @param v
     * @return
     */
    public static final byte[] intToReg(int v) {
        byte[] regs = new byte[4];
        regs[0] = (byte) (0xff & (v >> 24));
        regs[1] = (byte) (0xff & (v >> 16));
        regs[2] = (byte) (0xff & (v >> 8));
        regs[3] = (byte) (0xff & v);
        return regs;
    }

    /**
     * 字节数组转long
     *
     * @param bytes
     * @return
     */
    public static final long regToLong(byte[] bytes) {
        return ((((long) (bytes[0] & 0xff) << 56)
                | ((long) (bytes[1] & 0xff) << 48)
                | ((long) (bytes[2] & 0xff) << 40)
                | ((long) (bytes[3] & 0xff) << 32)
                | ((long) (bytes[4] & 0xff) << 24)
                | ((long) (bytes[5] & 0xff) << 16)
                | ((long) (bytes[6] & 0xff) << 8) | ((long) (bytes[7] & 0xff))));
    }

    /**
     * long转字节数组
     *
     * @param v
     * @return
     */
    public static final byte[] longToReg(long v) {
        byte[] regs = new byte[8];
        regs[0] = (byte) (0xff & (v >> 56));
        regs[1] = (byte) (0xff & (v >> 48));
        regs[2] = (byte) (0xff & (v >> 40));
        regs[3] = (byte) (0xff & (v >> 32));
        regs[4] = (byte) (0xff & (v >> 24));
        regs[5] = (byte) (0xff & (v >> 16));
        regs[6] = (byte) (0xff & (v >> 8));
        regs[7] = (byte) (0xff & v);
        return regs;
    }

    /**
     * 字节数组转float
     *
     * @param bytes
     * @return
     */
    public static final float regToFloat(byte[] bytes) {
        return Float
                .intBitsToFloat((((bytes[0] & 0xff) << 24)
                        | ((bytes[1] & 0xff) << 16) | ((bytes[2] & 0xff) << 8) | (bytes[3] & 0xff)));
    }

    /**
     * float转字节数组
     *
     * @param f
     * @return
     */
    public static final byte[] floatToReg(float f) {
        return intToReg(Float.floatToIntBits(f));
    }

    /**
     * 字节数组转double
     *
     * @param bytes
     * @return
     */
    public static final double regToDouble(byte[] bytes) {
        return Double
                .longBitsToDouble(((((long) (bytes[0] & 0xff) << 56)
                        | ((long) (bytes[1] & 0xff) << 48)
                        | ((long) (bytes[2] & 0xff) << 40)
                        | ((long) (bytes[3] & 0xff) << 32)
                        | ((long) (bytes[4] & 0xff) << 24)
                        | ((long) (bytes[5] & 0xff) << 16)
                        | ((long) (bytes[6] & 0xff) << 8) | ((long) (bytes[7] & 0xff)))));
    }

    /**
     * double转字节数组
     *
     * @param d
     * @return
     */
    public static final byte[] doubleToReg(double d) {
        return longToReg(Double.doubleToLongBits(d));
    }

    /**
     * byte转int
     *
     * @param b
     * @return
     */
    public static final int unsignedByteToInt(byte b) {
        return (int) b & 0xFF;
    }// unsignedByteToInt

}

记录如上!加油

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值