我有大约 200 个字符的字符串,包括字符和符号我想使用任何算法压缩这个字符串......
请帮助我任何类型的程序、代码、算法
提前致谢
目前我正在使用它,但是当符号存在时,它显示数组索引超出范围。
**COMPRESSION**
byte[] encode(String txt, int bit){
int length = txt.length();
float tmpRet1=0,tmpRet2=0;
if(bit==6){
tmpRet1=3.0f;
tmpRet2=4.0f;
}else if(bit==5){
tmpRet1=5.0f;
tmpRet2=8.0f;
}
byte encoded[]=new byte[(int)(tmpRet1*Math.ceil(length/tmpRet2))];
char str[]=new char[length];
txt.getChars(0,length,str,0);
int chaVal = 0;
String temp;
String strBinary = new String("");
for (int i = 0;i<length; i++){
temp = Integer.toBinaryString(toValue(str[i]));
while(temp.length()%bit != 0){
temp="0"+temp;
}
strBinary=strBinary+temp;
}
while(strBinary.length()%8 != 0){
strBinary=strBinary+"0";
}
Integer tempInt =new Integer(0);
for(int i=0 ; i<strBinary.length();i=i+8){
tempInt = tempInt.valueOf(strBinary.substring(i,i+8),2);
encoded[i/8]=tempInt.byteValue();
}
return encoded;
}
**DECOMPRESSION** :
String decode(byte[] encoded, int bit){
String strTemp = new String("");
String strBinary = new String("");
String strText = new String("");
Integer tempInt =new Integer(0);
int intTemp=0;
for(int i = 0;i<encoded.length;i++){
if(encoded[i]<0){
intTemp = (int)encoded[i]+256;
}else
intTemp = (int)encoded[i];
strTemp = Integer.toBinaryString(intTemp);
while(strTemp.length()%8 != 0){
strTemp="0"+strTemp;
}
strBinary = strBinary+strTemp;
}
for(int i=0 ; i<strBinary.length();i=i+bit){
tempInt = tempInt.valueOf(strBinary.substring(i,i+bit),2);
strText = strText + toChar(tempInt.intValue());
}
return strText;
}