Android 大端模式转换

public static byte[] toLH(int n) {
    byte[] b = new byte[4];
    b[0] = (byte) (n >> 0 & 0xff);
    b[1] = (byte) (n >> 8 & 0xff);
    b[2] = (byte) (n >> 16 & 0xff);
    b[3] = (byte) (n >> 24 & 0xff);
    return b;
}

public static byte[] DoubleToBytes(double d){
    //根据 IEEE 754 浮点“双精度格式”位布局,返回指定浮点值的表示形式,并保留 NaN 值。
    Long value = Double.doubleToRawLongBits(d);
    byte[] b = new byte[8];
    for(int i = 0 ; i<8;i++){
        b[i] = (byte)((value>>8*i)&0xff);
    }
    return b;
}

public static String reverseCharArray(String s) {
    if (s == null)
        s = "";
    char[] array = s.toCharArray();
    String reverse = "";
    for (int i = array.length - 1; i >= 0; i--) {
        reverse += array[i];
    }
    return reverse;
}

public static byte[] byteMerger(byte[] data) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        os.write(data.length);
        os.write(data);
    } catch (IOException e) {
        e.printStackTrace();
    }
    AppLog.e(DensityUtil.class, "byteMerger byte  arrry length ---> " + os.toByteArray().length);
    return os.toByteArray();
}


public static int convertByteArrToInt(byte[] byteArr, int index) {
    int intNum = 1;
    int intResult = 0;
    int ch1, ch2, ch3, ch4;
    //for(int k=index; j<intArr.length; j++, k+=4){
    int k = index;
    ch1 = byteArr[k];
    ch2 = byteArr[k + 1];
    ch3 = byteArr[k + 2];
    ch4 = byteArr[k + 3];
    if (ch1 < 0) {
        ch1 = 256 + ch1;
    }
    if (ch2 < 0) {
        ch2 = 256 + ch2;
    }
    if (ch3 < 0) {
        ch3 = 256 + ch3;
    }
    if (ch4 < 0) {
        ch4 = 256 + ch4;
    }
    intResult = (ch1) + (ch2 << 8) + (ch3 << 16) + (ch4 << 24);

    return intResult;
}

public static byte[] convertStrToByteArr(String str) {
    byte[] bs = str.getBytes();
    return bs;
}

public static String convertByteArrToString(byte[] byteArr, int index, int len) {
    int newlen = 0;
    for (int i = index; i < index + len; i++) {
        if (byteArr[i] == 0)
            break;
        newlen += 1;
    }
    if (newlen == 0)
        return null;
    String str = new String(byteArr, index, newlen);
    return str;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值