大小端转换

import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public class FormatTransfer {
    /**
     * 将int转为低字节在前,高字节在后的byte数组
     *
     * @param n int
     * @return byte[]
     */
    public static byte[] toLH(int n) {
        byte[] b = new byte[4];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        b[2] = (byte) (n >> 16 & 0xff);
        b[3] = (byte) (n >> 24 & 0xff);
        return b;
    }

    public static String bytesToString(byte[] b) {
        StringBuffer result = new StringBuffer("");
        int length = b.length;
        for (int i = 0; i < length; i++) {
            result.append((char) (b[i] & 0xff));
        }
        return result.toString();
    }

    /**
     * 将低字节数组转换为int
     *
     * @param b byte[]
     * @return int
     */
    public static int lBytesToInt(byte[] b) {
        int s = 0;
        for (int i = 0; i < 3; i++) {
            if (b[3 - i] >= 0) {
                s = s + b[3 - i];
            } else {
                s = s + 256 + b[3 - i];
            }
            s = s * 256;
        }
        if (b[0] >= 0) {
            s = s + b[0];
        } else {
            s = s + 256 + b[0];
        }
        return s;
    }

    public static long bytes2Long(byte[] byteNum) {
        long num = 0;
        for (int ix = 0; ix < 8; ++ix) {
            num <<= 8;
            num |= (byteNum[ix] & 0xff);
        }
        return num;
    }

    public static int hBytesToInt(byte[] src, int offset) {
        int value;
        value = (int) ( ((src[offset] & 0xFF)<<24)
                |((src[offset+1] & 0xFF)<<16)
                |((src[offset+2] & 0xFF)<<8)
                |(src[offset+3] & 0xFF));
        return value;
    }

    public static int getIntFromByteBuffer(ByteBuffer buf, int offset) {
        buf.position(offset);
        buf.limit(offset + 4);
        byte[] bytes = new byte[4];
        buf.get(bytes);
        return lBytesToInt(bytes);
    }

    public static boolean getBooleanFromByteBuffer(ByteBuffer buf, int offset) {
        buf.position(offset);
        buf.limit(offset + 1);
        byte[] bytes = new byte[1];
        buf.get(bytes);
        return (bytes[0] & 0x01)==1 ? true : false;
    }

    public static String getStringFromByteBuffer(ByteBuffer buf,
                                                 int offset, int len) {
        buf.position(offset);
        buf.limit(offset + len);
        byte[] bytes = new byte[len];
        buf.get(bytes);
        try {
            return new String(bytes, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static byte[] getBytesFromByteBuffer(ByteBuffer buf,
                                                int offset, int len) {
        buf.position(offset);
        buf.limit(offset + len);
        byte[] bytes = new byte[len];
        buf.get(bytes);
        return bytes;
    }

    static public byte[] getBytes(char[] chars) {
        byte[] buf = new byte[chars.length];
        for (int i = 0; i < chars.length; i++) {
            buf[i] = (byte) chars[i];
        }
        return buf;
    }

    static public void putStringToByteBuffer(ByteBuffer buf, String str, int maxLen) {
        if (TextUtils.isEmpty(str) || str.getBytes().length > maxLen) {
            buf.put(new byte[maxLen]);
        } else {
            buf.put(str.getBytes());
            buf.put(new byte[maxLen - str.getBytes().length]);
        }
    }

    /**
     * int转为二进制 str
     *
     * @param int num
     * @return String
     */
    public static String lIntToStrBinary(int num) {
        String str_Binary = "";
        while (num != 0){
            str_Binary = str_Binary + num % 2;
            num = num / 2;
        }

        if(str_Binary.length() < 7){
            int len = str_Binary.length();
            for(int i = 0; i < 7-len ; i ++) {
                str_Binary = str_Binary + "0";
            }
        }

        return str_Binary;
    }

    public static short byteToShort(byte[] b) {
        short s = 0;
        short s0 = (short) (b[0] & 0xff);// 最低位
        short s1 = (short) (b[1] & 0xff);
        s1 <<= 8;
        s = (short) (s0 | s1);
        return s;

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值