Android 数据之间转换

Hex字符串转int

static public int HexToInt(String inHex)
{
       return Integer.parseInt(inHex, 16);
}

Hex字符串转byte

static public byte HexToByte(String inHex)
    {
        return (byte)Integer.parseInt(inHex,16);
    }

1字节转2个Hex字符

static public String Byte2Hex(Byte inByte)//1字节转2个Hex字符
    {
        return String.format("%02x", inByte).toUpperCase();
    }

字节数组转转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<Integer> list) {
    if (list == null || list.size() < 0)
        return null;
    int[] ints = new int[list.size()];
    int i = 0;
    Iterator<Integer> iterator = list.iterator();
    while (iterator.hasNext()) {
        ints[i] = iterator.next();
        i++;
    }
    return ints;
}

list --> byte[]

list<Byte> --> byte[]
private static byte[] list2byte(List<Byte> list) {
    if (list == null || list.size() < 0)
        return null;
    byte[] bytes = new byte[list.size()];
    int i = 0;
    Iterator<Byte> 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最乱纷飞的code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值