在通信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; } 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; }