bit7 转换String

public static String hexString7ToString(String s){
		if (StringUtils.isEmpty(s)) {
			throw new IllegalArgumentException("this hexString must not be empty");
		}
		byte[] dataArray = hexStr2ByteArray(s);
		String str = decode7bit(dataArray);
		return  str;

	}

	/**
	 * 7-bit解码
	 * @param d
	 * @return
	 */
	public static String decode7bit(byte[] d){
		byte[] other_mask ={(byte) 0x80,(byte) 0xc0,(byte) 0xe0,(byte) 0xf0,(byte) 0xf8,(byte) 0xfc,(byte) 0xfe};
		byte[] my_mask = {0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01};
		byte other = 0;
		byte temp = 0;
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<d.length;i++){
			int index = i%7;
			temp = d[i];
			d[i] = (byte) (d[i]&my_mask[index]);
			d[i] = (byte) (d[i] << index);
			if(index != 0){
				d[i] = (byte) (d[i]&other_mask[7-index]);
				other = (byte) (other>>(8-index));
				other = (byte) (other&my_mask[7-index]);
				d[i] = (byte) (d[i] | other);
			}
			other = (byte) (temp&other_mask[index]);
			sb.append((char)d[i]);
			if(index == 6){
				other = (byte) (other>>1);
				other = (byte) (other & 0x7f);
				sb.append((char)other);
			}
		}
		return sb.toString();
	}

	public static byte[] hexStr2ByteArray(String hexString) {
		if (StringUtils.isEmpty(hexString)) {
			throw new IllegalArgumentException("this hexString must not be empty");
		}
		hexString = hexString.toLowerCase();
		final byte[] byteArray = new byte[hexString.length() / 2];
		int k = 0;
		for (int i = 0; i < byteArray.length; i++) {
			byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
			byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
			byteArray[i] = (byte) (high << 4 | low);
			k += 2;
		}
		return byteArray;
	}

参考:GSM MODEM JAVA解码PDU短信 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值