ISOUtils.java 2.07 KB
package com.glsx.pushsdk.tools;

public class ISOUtils {
    public static final String TAG = "ISOUtils";

    public static byte[] short2Bytes(short value) {

        byte[] byteArrayData = new byte[2];
        byteArrayData[0] = (byte) (value & 0xff);
        byteArrayData[1] = (byte) ((value >> 8) & 0xff);

        return byteArrayData;
    }

    public static short bytes2Short(byte[] s) {
        short r = 0;
        for (int i = s.length - 1; i >= 0; i--) {
            r <<= 8;
            r |= (s[i] & 0x00ff);
        }
        return r;

    }

    // 字节长度专用转换
    public static byte[] letShort2Bytes1(short value) {

        byte[] byteArrayData = new byte[2];
        byteArrayData[0] = (byte) ((value >> 8) & 0xff);
        byteArrayData[1] = (byte) (value & 0xff);

        return byteArrayData;
    }

    public static byte[] int2bytes(int n) {
        byte[] ab = new byte[4];
        ab[0] = (byte) (0xff & n);
        ab[1] = (byte) ((0xff00 & n) >> 8);
        ab[2] = (byte) ((0xff0000 & n) >> 16);
        ab[3] = (byte) ((0xff000000 & n) >> 24);
        return ab;
    }

    public static int bytes2Int(byte[] bytes) {
        int addr = bytes[0] & 0xFF;
        addr |= ((bytes[1] << 8) & 0xFF00);
        addr |= ((bytes[2] << 16) & 0xFF0000);
        addr |= ((bytes[3] << 24) & 0xFF000000);
        return addr;
    }

    /**
     * 字节长度专用转换
     *
     * @param n int
     * @return 返回byte[]
     */
    public static byte[] lenInt2bytes1(int n) {
        byte[] ab = new byte[4];
        ab[0] = (byte) ((0xff00 & n) >> 8);
        ab[1] = (byte) (0xff & n);
        ab[2] = (byte) ((0xff0000 & n) >> 16);
        ab[3] = (byte) ((0xff000000 & n) >> 24);
        return ab;
    }

    /**
     * 字节长度专用转换
     *
     * @param bytes byte[]
     * @return 返回int
     */
    public static int lenBytes2Int(byte[] bytes) {
        int addr = ((bytes[0] << 8) & 0xFF00);
        addr |= bytes[1] & 0xFF;
        addr |= ((bytes[2] << 16) & 0xFF0000);
        addr |= ((bytes[3] << 24) & 0xFF000000);
        return addr;
    }

}