16位无符号数(unsigned short)转10进制

16位无符号数(unsigned short)转10进制://信号强度采用16位无符号数(unsigned short),数值越大信号越强, 单位db

0x00,0x19 -> 25db

16位unsigned short为2Byte,且无符号,则为0*16^3 + 0*16^2 + 1*16^1 + 9*16^0 = 25

思路是:(0x00,0x19)对应的高位为num[high]=0x00=0,低位为num[low]=0x19=25,要先把(0,25)->(0019)-,然后再计算结果。


//0x00,0x19 -> 入参为:0,25
private String toHexStr(int highByte, int lowByte){
	StringBuilder sb = new StringBuilder();
	if (highByte > 0) {
		String high = Integer.toHexString(highByte);
		if (high.length() < 2) {
			sb.append('0').append(high);
		} else {
			sb.append(high);
		}
	} else {
		sb.append("00");
	}

	if (lowByte > 0) {
		String low = Integer.toHexString(lowByte);
		if (low.length() < 2) {
			sb.append('0').append(low);
		} else {
			sb.append(low);
		}
	} else {
		sb.append("00");
	}
	return sb.toString();
}
 

//16进制的字符串 转 10进制的数
private int toDexNum(String hexStr){
	int num = 0;
	int numLen = hexStr.length();

	for(int i = 0; i < numLen; i++) {
		int temp1 = hexChar2DexNum(hexStr.charAt(i));
		int temp2 = (int)Math.pow(16, numLen-i-1);
		num += temp1 * temp2;
	}
//        Log.d(TAG, "toUnsignedShort: " + num + ", " + hexStr);
	return num;
}

//16进制的字符 转 10进制的数 
private int hexChar2DexNum(char ch){
	ch = Character.toLowerCase(ch);
	int temp = 0;
	if (ch >= '0' && ch <= '9'){
		temp = ch - '0';
	} else if (ch >= 'a' && ch <= 'f'){
		temp = ch - 'a' + 10;
	}
//        Log.d(TAG, "hexChar2DexNum: ch " + ch + ", num = " + temp);
	return temp;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值