java字节运算,拼接两个byte和和将一个int拆分成两个byte,byte取高位和取低位运算。

import java.text.DecimalFormat;

/**
 * Created by zry on 18/3/7.
 */

public class DataUtil {



    /**
     * 保留浮点数小数点后面几位
     *
     * @param f      浮点数
     * @param format 浮点数的格式
     * @return
     */
    public static String formatFloat(float f, String format) {
        DecimalFormat df = new DecimalFormat(format);
        return df.format(f);
    }

    /**
     * 将两个字节拼接还原成有符号的整型数据
      数据域中的数据都按小端存储,示例:数据0x1234,则Byte0为0x34,Byte1为0x12
     *
     * @param byte1
     * @param byte2
     * @return
     */
    public static int pinJie2ByteToInt(byte byte1, byte byte2) {
        int result = byte1;
        result = (result << 8) | (0x00FF & byte2);
        return result;
    }

    /**
     * 整型数据拆分为长度为2的字节数组,低8位存放在序号小的元素,高8位存放在序号大的元素中(小端存储)
     *
     * @param data
     * @return
     */
    public static byte[] chaiFenDataIntTo2Byte(int data) {
        byte[] byteArray = new byte[2];
        byteArray[0] = (byte) data;
        byteArray[1] = (byte) (data >> 8);
        return byteArray;
    }


    /**
     * 将byte数组转成十六进制形式的字符串
     *
     * @param src
     * @return
     */
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (byte aSrc : src) {
            int v = aSrc & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
            stringBuilder.append(' ');
        }
        return stringBuilder.toString();
    }

    /**
     * 取字节的后5位[4:0]表示斜率
     *
     * @param xoctByte
     * @return
     */
    public static int getXoctInt(byte xoctByte) {

        //或上 & 0x1F 取后五位
        int result = xoctByte & 0x1F;
        return result;

    }

    /**
     * 将斜率整型0-8 添加到字节的[4:0]位上去
     *
     * @param xoctInt
     * @return
     */
    public static byte setIntXoctToByte(byte xoctByte, int xoctInt) {

        //先取int的后五位保留
        byte byteInt = (byte) xoctInt;
        byteInt = (byte) (byteInt & 0x1F);//取后五位

        //保留原先byte的前两位
        xoctByte = (byte) (xoctByte & 0xC0);

        //将前两位与后五位拼接形成一个新的,按需求要的byte

        xoctByte = (byte) (xoctByte | byteInt);

        return xoctByte;

    }

    /**
     * 取字节的前两位[7:6]表示类型
     *
     * @param xoctByte
     * @return
     */
    public static int getTypeInt(byte xoctByte) {

        int result = (xoctByte >> 6) & 0x03;
        return result;

    }

    /**
     * 将斜率整型0-3 添加到字节的[7:06]位上去
     *
     * @param xoctInt
     * @return
     */
    public static byte setIntTypeToByte(byte xoctByte, int xoctInt) {


        byte byteInt = (byte) xoctInt;
        byteInt = (byte) ((byteInt & 0x03) << 6);

        //保留原先byte的后五位
        xoctByte = (byte) (xoctByte & 0x1F);

        //将前两位与后五位拼接形成一个新的,按需求要的byte

        xoctByte = (byte) (xoctByte | byteInt);

        return xoctByte;

    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值