Android 数据之间转换

字节数组转转hex字符串

static public String ByteArrToHex(byte[] inBytArr)//字节数组转转hex字符串    {        StringBuilder strBuilder=new StringBuilder();        int j=inBytArr.length;        for (int i = 0; i < j; i++)        {            strBuilder.append(Byte2Hex(inBytArr[i]));            strBuilder.append(" ");        }        return strBuilder.toString();    }

以小端模式将int转成byte[]

/**     * 以小端模式将int转成byte[]     *     * @param value     * @return     */    public static byte[] intToBytesLittle(int value) {        byte[] src = new byte[4];        src[3] = (byte) ((value >> 24) & 0xFF);        src[2] = (byte) ((value >> 16) & 0xFF);        src[1] = (byte) ((value >> 8) & 0xFF);        src[0] = (byte) (value & 0xFF);        return src;    }

以大端模式将int转成byte[]

/**     * 以大端模式将int转成byte[]     */    public static byte[] intToBytesBig(int value) {        byte[] src = new byte[4];        src[0] = (byte) ((value >> 24) & 0xFF);        src[1] = (byte) ((value >> 16) & 0xFF);        src[2] = (byte) ((value >> 8) & 0xFF);        src[3] = (byte) (value & 0xFF);        return src;    }

list --> int[]

private static int[] list2int(List list) {    if (list == null || list.size() < 0)        return null;    int[] ints = new int[list.size()];    int i = 0;    Iterator iterator = list.iterator();    while (iterator.hasNext()) {        ints[i] = iterator.next();        i++;    }    return ints;}

list --> byte[]

list --> byte[]private static byte[] list2byte(List list) {    if (list == null || list.size() < 0)        return null;    byte[] bytes = new byte[list.size()];    int i = 0;    Iterator iterator = list.iterator();    while (iterator.hasNext()) {        bytes[i] = iterator.next();        i++;    }    return bytes;}

byte[] --> int[]

public static int[] bytes2Int(byte[] bytes) {    if (bytes == null || bytes.length < 1) {        return null;    }    int[] ints = new int[bytes.length];    for (int i = 0; i < bytes.length; i++) {        ints[i] = bytes[i] < 0 ? bytes[i] & 255 : bytes[i];    }    return ints;}

byte --> int

public static int byte2Int(byte b) {    return b < 0 ? b & 255 : b;}
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章