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;
}
bit7 转换String
最新推荐文章于 2023-10-03 17:15:44 发布