模拟
PPP
协议
0
比特填充
import
java.io.UnsupportedEncodingException;
/**
*
编码类
*
功能:模拟
PPP
协议字节填充的编码过程
*/
public
class
Code {
//
把字符串的长度填充到
8
private
String fillEight(String str) {
if
(str.length()
str =
"0"
+ str;
str = fillEight(str);
}
return
str;
}
//
返回数据部分二进制形式的字符串
private
String getDataByte(
byte
[] b) {
String str =
""
;
for
(
int
temp : b) {
temp &= 0xff;
//
将高
24
位变成全
0
str += fillEight(Integer.
toBinaryString
(temp));
}
return
str;
}
//
对字符串进行字节填充
private
String fillByte(String str) {
int
count = 0;
//
计算连续的
1
的个数
for
(
int
i = 0; i
if
(str.charAt(i) ==
'1'
) {
count++;
}
else
{
count = 0;
}
if
(count == 5) {
str = str.substring(0, i + 1) +
"0"
+ fillByte(str.substring(i + 1));
return
str;
}
}
return
str;
}
//
对数据进行封装
private
String packaging(String str) {
if
(str.length() > 1500) {
2534

被折叠的 条评论
为什么被折叠?



