JAvaバイト演算、2つのbyteをつなぎ合わせ、1つのintを2つのbyteに分割し、byteは上位と下位の演算をとる.

3132 ワード

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;

    }


}