您的位置:首页 > 编程语言 > Java开发

java类型 7bit字节与String 相互转换

2018-04-03 09:10 447 查看
在通信GSM,俗称的2G 通常采用的是7bit编码,是一种7位编码压缩算法,其实原理上来说一个字节占8个比特位,一般最高位都是符号运算位 ,通常忽略不计,7bit其实就是运用了最高位将字节往前移一位 就能空出一个比特位来给下个字节用,也就是说8个字节  之前的话 64位比特位来表达  现在 56位就足够少8个字节
这个是7bit转换为String代码
public static String Gsm7BitDecode(byte[] lpBuf, byte[] lpUserData, int len)
{
int byPosition,byHighPart,byLowPart;
int byCycle;
int bycle =0;
byLowPart = 0;
for(byCycle = 0; byCycle<len; byCycle++) {

byPosition = byCycle % 7;
byHighPart = (lpBuf[byCycle]) & BitAnd((byte)(7-byPosition));
lpUserData[bycle] = (byte) ((byHighPart << byPosition) + byLowPart);

byLowPart = (lpBuf[byCycle] >> (byte)(7-byPosition)) & BitAnd((byte) (byPosition+1));
if(byPosition == 6)
{
bycle++;
lpUserData[bycle] = (byte) byLowPart;
byLowPart = 0;
}
bycle++;
}
String s = new String(lpUserData);
return s;
}
这是String 转换为7bit的编码
private static byte[] get7Bit(String strContent) {
// 结果
byte[] arrResult = null;
try {
// 编码方式
byte[] arrs = strContent.getBytes("ASCII");
System.out.println(new String(arrs));

arrResult = new byte[arrs.length - (arrs.length / 8)];
int intRight = 0;
int intLeft = 7;
int intIndex = 0;
for (int i = 1; i <= arrs.length; i++, intRight++, intLeft--) {
if (i % 8 == 0) {
intRight = -1;
intLeft = 8;
continue;
}

4000
byte newItem = 0;
if (i == arrs.length) {
newItem = (byte) (arrs[i - 1] >> intRight);
} else {
newItem = (byte) ((arrs[i - 1] >> intRight) | (arrs[i] << intLeft));
}

arrResult[intIndex] = newItem;
intIndex++;

}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return arrResult;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: