Base64是将3个字节按6位取变成4个新的字节.
我现在自己写的一个Base32编码,将5个字节按5位取变成8个新的字节.
英文跟数字都没有问题,但是中文就不支持.我不知道什么原因.
Base64好象有已经封装好的编码解码方法!但不知道它是怎么处理中文的
下面是我的代码!麻烦高手帮我看看哪有问题??????import java.io.*;
import java.util.*;public class Check1
{
private int base32_map_length = 32;
private char[] ch_base32_map =
{
'A', 'V', 'F', '8', 'E', 'C', 'R', 'H', // 0-7
'I', 'T', 'K', 'L', 'M', 'N', 'O', 'U', // 8-15
'Q', 'G', 'S', 'J', '4', 'B', 'W', 'X', // 16-23
'Y', 'Z', '9', 'D', '7', '6', '5', 'P', // 24-31
};
private int GetCharIndex(char ch)
{
int i = 0;
for(;i<ch_base32_map.length;i++)
{
if(ch == ch_base32_map[i]) break;
}
return i;
}
//Base32编码
public String encode(byte[] by)
{
int i = 0,char_index = 0,char_count;
char[] acPart = null;
StringBuffer output = null;
if (by == null || ch_base32_map == null || ch_base32_map.length < base32_map_length)
return null;
try
{
char_count = (int) (by.length / 5f * 8f) + 1;
output = new StringBuffer(char_count);
acPart = new char[8];
}
catch (Exception e)
{
e.printStackTrace();
}
if (acPart == null || output == null)
return null;
char_count = 0;
for(i = 0;i < by.length;i += 5)
{
//AAAAABBB BBCCCCCD DDDDEEEE EFFFFFGG GGGHHHHH
switch (by.length - i)
{
case 1:
char_index = (by[i] & 0xF8) >> 3; // AAAAA
acPart[0] = ch_base32_map[char_index];
char_index = (by[i] & 0x7) << 2; // BBB00
acPart[1] = ch_base32_map[char_index];
char_count = 2;
break;
case 2:
char_index = (by[i] & 0xF8) >> 3; // AAAAA
acPart[0] = ch_base32_map[char_index];
char_index = ((by[i] & 0x7) << 2) | (by[i + 1] >> 6); // BBBBB
acPart[1] = ch_base32_map[char_index];
char_index = (by[i + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = ch_base32_map[char_index];
char_index = by[i + 1] & 0x1; // D0000
acPart[3] = ch_base32_map[char_index];
char_count = 4;
break;
case 3:
char_index = (by[i] & 0xF8) >> 3; // AAAAA
acPart[0] = ch_base32_map[char_index];
char_index = ((by[i] & 0x7) << 2) | (by[i + 1] >> 6); // BBBBB
acPart[1] = ch_base32_map[char_index];
char_index = (by[i + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = ch_base32_map[char_index];
char_index = ((by[i + 1] & 0x1) << 4) | (by[i + 2] >> 4);// DDDDD
acPart[3] = ch_base32_map[char_index];
char_index = (by[i + 2] & 0xF) << 1; // EEEE0
acPart[4] = ch_base32_map[char_index];
char_count = 5;
break;
case 4:
char_index = (by[i] & 0xF8) >> 3; // AAAAA
acPart[0] = ch_base32_map[char_index];
char_index = ((by[i] & 0x7) << 2) | (by[i + 1] >> 6); // BBBBB
acPart[1] = ch_base32_map[char_index];
char_index = (by[i + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = ch_base32_map[char_index];
char_index = ((by[i + 1] & 0x1) << 4) | (by[i + 2] >> 4);// DDDDD
acPart[3] = ch_base32_map[char_index];
char_index = ((by[i + 2] & 0xF) << 1) | (by[i + 3] >> 7);// EEEEE
acPart[4] = ch_base32_map[char_index];
char_index = (by[i + 3] & 0x7F) >> 2; // FFFFF
acPart[5] = ch_base32_map[char_index];
char_index = (by[i + 3] & 0x3) << 3; // GG000
acPart[6] = ch_base32_map[char_index];
char_count = 7;
break;
default: // >= 5
char_index = (by[i] & 0xF8) >> 3; // AAAAA
acPart[0] = ch_base32_map[char_index];
char_index = ((by[i] & 0x7) << 2) | (by[i + 1] >> 6); // BBBBB
acPart[1] = ch_base32_map[char_index];
char_index = (by[i + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = ch_base32_map[char_index];
char_index = ((by[i + 1] & 0x1) << 4) | (by[i + 2] >> 4);// DDDDD
acPart[3] = ch_base32_map[char_index];
char_index = ((by[i + 2] & 0xF) << 1) | (by[i + 3] >> 7);// EEEEE
acPart[4] = ch_base32_map[char_index];
char_index = (by[i + 3] & 0x7F) >> 2; // FFFFF
acPart[5] = ch_base32_map[char_index];
char_index = ((by[i + 3] & 0x3) << 3) | (by[i + 4] >> 5);// GGGGG
acPart[6] = ch_base32_map[char_index];
char_index = by[i + 4] & 0x1F; // HHHHH
acPart[7] = ch_base32_map[char_index];
char_count = 8;
break;
}
output.append(acPart,0,char_count);
}
return output.toString();
}
我现在自己写的一个Base32编码,将5个字节按5位取变成8个新的字节.
英文跟数字都没有问题,但是中文就不支持.我不知道什么原因.
Base64好象有已经封装好的编码解码方法!但不知道它是怎么处理中文的
下面是我的代码!麻烦高手帮我看看哪有问题??????import java.io.*;
import java.util.*;public class Check1
{
private int base32_map_length = 32;
private char[] ch_base32_map =
{
'A', 'V', 'F', '8', 'E', 'C', 'R', 'H', // 0-7
'I', 'T', 'K', 'L', 'M', 'N', 'O', 'U', // 8-15
'Q', 'G', 'S', 'J', '4', 'B', 'W', 'X', // 16-23
'Y', 'Z', '9', 'D', '7', '6', '5', 'P', // 24-31
};
private int GetCharIndex(char ch)
{
int i = 0;
for(;i<ch_base32_map.length;i++)
{
if(ch == ch_base32_map[i]) break;
}
return i;
}
//Base32编码
public String encode(byte[] by)
{
int i = 0,char_index = 0,char_count;
char[] acPart = null;
StringBuffer output = null;
if (by == null || ch_base32_map == null || ch_base32_map.length < base32_map_length)
return null;
try
{
char_count = (int) (by.length / 5f * 8f) + 1;
output = new StringBuffer(char_count);
acPart = new char[8];
}
catch (Exception e)
{
e.printStackTrace();
}
if (acPart == null || output == null)
return null;
char_count = 0;
for(i = 0;i < by.length;i += 5)
{
//AAAAABBB BBCCCCCD DDDDEEEE EFFFFFGG GGGHHHHH
switch (by.length - i)
{
case 1:
char_index = (by[i] & 0xF8) >> 3; // AAAAA
acPart[0] = ch_base32_map[char_index];
char_index = (by[i] & 0x7) << 2; // BBB00
acPart[1] = ch_base32_map[char_index];
char_count = 2;
break;
case 2:
char_index = (by[i] & 0xF8) >> 3; // AAAAA
acPart[0] = ch_base32_map[char_index];
char_index = ((by[i] & 0x7) << 2) | (by[i + 1] >> 6); // BBBBB
acPart[1] = ch_base32_map[char_index];
char_index = (by[i + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = ch_base32_map[char_index];
char_index = by[i + 1] & 0x1; // D0000
acPart[3] = ch_base32_map[char_index];
char_count = 4;
break;
case 3:
char_index = (by[i] & 0xF8) >> 3; // AAAAA
acPart[0] = ch_base32_map[char_index];
char_index = ((by[i] & 0x7) << 2) | (by[i + 1] >> 6); // BBBBB
acPart[1] = ch_base32_map[char_index];
char_index = (by[i + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = ch_base32_map[char_index];
char_index = ((by[i + 1] & 0x1) << 4) | (by[i + 2] >> 4);// DDDDD
acPart[3] = ch_base32_map[char_index];
char_index = (by[i + 2] & 0xF) << 1; // EEEE0
acPart[4] = ch_base32_map[char_index];
char_count = 5;
break;
case 4:
char_index = (by[i] & 0xF8) >> 3; // AAAAA
acPart[0] = ch_base32_map[char_index];
char_index = ((by[i] & 0x7) << 2) | (by[i + 1] >> 6); // BBBBB
acPart[1] = ch_base32_map[char_index];
char_index = (by[i + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = ch_base32_map[char_index];
char_index = ((by[i + 1] & 0x1) << 4) | (by[i + 2] >> 4);// DDDDD
acPart[3] = ch_base32_map[char_index];
char_index = ((by[i + 2] & 0xF) << 1) | (by[i + 3] >> 7);// EEEEE
acPart[4] = ch_base32_map[char_index];
char_index = (by[i + 3] & 0x7F) >> 2; // FFFFF
acPart[5] = ch_base32_map[char_index];
char_index = (by[i + 3] & 0x3) << 3; // GG000
acPart[6] = ch_base32_map[char_index];
char_count = 7;
break;
default: // >= 5
char_index = (by[i] & 0xF8) >> 3; // AAAAA
acPart[0] = ch_base32_map[char_index];
char_index = ((by[i] & 0x7) << 2) | (by[i + 1] >> 6); // BBBBB
acPart[1] = ch_base32_map[char_index];
char_index = (by[i + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = ch_base32_map[char_index];
char_index = ((by[i + 1] & 0x1) << 4) | (by[i + 2] >> 4);// DDDDD
acPart[3] = ch_base32_map[char_index];
char_index = ((by[i + 2] & 0xF) << 1) | (by[i + 3] >> 7);// EEEEE
acPart[4] = ch_base32_map[char_index];
char_index = (by[i + 3] & 0x7F) >> 2; // FFFFF
acPart[5] = ch_base32_map[char_index];
char_index = ((by[i + 3] & 0x3) << 3) | (by[i + 4] >> 5);// GGGGG
acPart[6] = ch_base32_map[char_index];
char_index = by[i + 4] & 0x1F; // HHHHH
acPart[7] = ch_base32_map[char_index];
char_count = 8;
break;
}
output.append(acPart,0,char_count);
}
return output.toString();
}
public byte[] decode(String sData)
{
int i = 0,dwLength = 0;
int[] char_index = null;
byte[] output = null;
char[] acInput = null;
if (sData == null || sData == "" || sData.equals(""))
return null;
acInput = sData.toCharArray();
if (acInput == null)
return null;
try
{
dwLength = (int)(acInput.length / 8f * 5f) + 1;
output = new byte[dwLength];
char_index = new int[8];
}
catch (Exception e)
{
e.printStackTrace();
}
if (acInput == null)
return null;
dwLength = 0;
for (i = 0;i < acInput.length;i += 8)
{
switch (acInput.length - i)
{
case 1:
char_index[0] = GetCharIndex(acInput[i]);
output[dwLength] = (byte) (char_index[0] << 3);
break;
case 2:
char_index[0] = GetCharIndex(acInput[i]);
char_index[1] = GetCharIndex(acInput[i + 1]);
output[dwLength] = (byte) ((char_index[0] << 3) | (char_index[1] >> 2));
output[dwLength + 1] = (byte) ((char_index[1] & 0x3) << 6);
break;
case 3:
char_index[0] = GetCharIndex(acInput[i]);
char_index[1] = GetCharIndex(acInput[i + 1]);
char_index[2] = GetCharIndex(acInput[i + 2]);
output[dwLength] = (byte) ((char_index[0] << 3) | (char_index[1] >> 2));
output[dwLength + 1] = (byte) (((char_index[1] & 0x3) << 6) | (char_index[2] << 1));
break;
case 4:
char_index[0] = GetCharIndex(acInput[i]);
char_index[1] = GetCharIndex(acInput[i + 1]);
char_index[2] = GetCharIndex(acInput[i + 2]);
char_index[3] = GetCharIndex(acInput[i + 3]);
output[dwLength] = (byte) ((char_index[0] << 3) | (char_index[1] >> 2));
output[dwLength + 1] = (byte) (((char_index[1] & 0x3) << 6) | (char_index[2] << 1) | (char_index[3] >> 4));
output[dwLength + 2] = (byte) ((char_index[3] & 0xF) << 4);
break;
case 5:
char_index[0] = GetCharIndex(acInput[i]);
char_index[1] = GetCharIndex(acInput[i + 1]);
char_index[2] = GetCharIndex(acInput[i + 2]);
char_index[3] = GetCharIndex(acInput[i + 3]);
char_index[4] = GetCharIndex(acInput[i + 4]);
output[dwLength] = (byte) (char_index[0] << 3 | char_index[1] >> 2);
output[dwLength + 1] = (byte) (((char_index[1] & 0x3) << 6) | (char_index[2] << 1) | (char_index[3] >> 4));
output[dwLength + 2] = (byte) (((char_index[3] & 0xF) << 4) | (char_index[4] >> 1));
output[dwLength + 3] = (byte) ((char_index[4] & 0x1) << 7);
break;
case 6:
char_index[0] = GetCharIndex(acInput[i]);
char_index[1] = GetCharIndex(acInput[i + 1]);
char_index[2] = GetCharIndex(acInput[i + 2]);
char_index[3] = GetCharIndex(acInput[i + 3]);
char_index[4] = GetCharIndex(acInput[i + 4]);
char_index[5] = GetCharIndex(acInput[i + 5]);
output[dwLength] = (byte) ((char_index[0] << 3) | (char_index[1] >> 2));
output[dwLength + 1] = (byte) (((char_index[1] & 0x3) << 6) | (char_index[2] << 1) | (char_index[3] >> 4));
output[dwLength + 2] = (byte) (((char_index[3] & 0xF) << 4) | (char_index[4] >> 1));
output[dwLength + 3] = (byte) (((char_index[4] & 0x1) << 7) | (char_index[5] << 2));
break;
case 7:
char_index[0] = GetCharIndex(acInput[i]);
char_index[1] = GetCharIndex(acInput[i + 1]);
char_index[2] = GetCharIndex(acInput[i + 2]);
char_index[3] = GetCharIndex(acInput[i + 3]);
char_index[4] = GetCharIndex(acInput[i + 4]);
char_index[5] = GetCharIndex(acInput[i + 5]);
char_index[6] = GetCharIndex(acInput[i + 6]);
output[dwLength] = (byte) ((char_index[0] << 3) | (char_index[1] >> 2));
output[dwLength + 1] = (byte) (((char_index[1] & 0x3) << 6) | (char_index[2] << 1) | (char_index[3] >> 4));
output[dwLength + 2] = (byte) (((char_index[3] & 0xF) << 4) | (char_index[4] >> 1));
output[dwLength + 3] = (byte) (((char_index[4] & 0x1) << 7) | (char_index[5] << 2) | (char_index[6] >> 3));
output[dwLength + 4] = (byte) ((char_index[6] & 0x7) << 5);
break;
default:
char_index[0] = GetCharIndex(acInput[i]);
char_index[1] = GetCharIndex(acInput[i + 1]);
char_index[2] = GetCharIndex(acInput[i + 2]);
char_index[3] = GetCharIndex(acInput[i + 3]);
char_index[4] = GetCharIndex(acInput[i + 4]);
char_index[5] = GetCharIndex(acInput[i + 5]);
char_index[6] = GetCharIndex(acInput[i + 6]);
char_index[7] = GetCharIndex(acInput[i + 7]); output[dwLength] = (byte) ((char_index[0] << 3) | (char_index[1] >> 2));
output[dwLength + 1] = (byte) (((char_index[1] & 0x3) << 6) | (char_index[2] << 1) | (char_index[3] >> 4));
output[dwLength + 2] = (byte) (((char_index[3] & 0xF) << 4) | (char_index[4] >> 1));
output[dwLength + 3] = (byte) (((char_index[4] & 0x1) << 7) | (char_index[5] << 2) | (char_index[6] >> 3));
output[dwLength + 4] = (byte) (((char_index[6] & 0x7) << 5) | (char_index[7]));
break;
}
dwLength += 5;
}
return output;
}public static void main(String[] args)
{
try
{
Check1 check1 = new Check1();
String str_base = "ABCGSYD";
String str = check1.encode(str_base.getBytes());
System.out.println("编码后="+str);
String str1 = new String(check1.decode(str));
System.out.println("解码后="+str1);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
我没有Base64编码的代码,你有吗?能不能给我发一份啊
我的E-MAIL [email protected]:bao110908(火龙果)
汉字转换成字节数组时都是负数.我不知道怎么处理这个!!!救救我啊...........................help~~~~~~~~~~~~
下面是改完的,可以处理任何字符,默认用的是UTF-8编码.
有的改的地方注释掉了,有的你和原来的对对.
代码如下:// Base32编码
public String encode(String inputString) throws UnsupportedEncodingException {
byte[] by = inputString.getBytes("UTF-8");
int i = 0, char_index = 0, char_count;
char[] acPart = null;
StringBuffer output = null; if (by == null || ch_base32_map == null
|| ch_base32_map.length < base32_map_length)
return null; try {
char_count = (int) (by.length / 5f * 8f) + 1;
output = new StringBuffer(char_count);
acPart = new char[8];
} catch (Exception e) {
e.printStackTrace();
}
if (acPart == null || output == null)
return null; char_count = 0;
for (i = 0; i < by.length; i += 5) {
// AAAAABBB BBCCCCCD DDDDEEEE EFFFFFGG GGGHHHHH
switch (by.length - i) {
case 1:
char_index = (by[i] & 0xF8) >> 3; // AAAAA
acPart[0] = ch_base32_map[char_index];
char_index = (by[i] & 0x7) << 2; // BBB00
acPart[1] = ch_base32_map[char_index];
char_count = 2;
break; case 2:
char_index = (by[i] & 0xF8) >> 3; // AAAAA
acPart[0] = ch_base32_map[char_index];
char_index = ((by[i] & 0x7) << 2) | ((by[i + 1] & 0xC0) >> 6); // BBBBB
acPart[1] = ch_base32_map[char_index];
char_index = (by[i + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = ch_base32_map[char_index];
char_index = (by[i + 1] & 0x1) << 4; // D0000
acPart[3] = ch_base32_map[char_index];
char_count = 4;
break; case 3:
char_index = (by[i] & 0xF8) >> 3; // AAAAA
acPart[0] = ch_base32_map[char_index];
char_index = ((by[i] & 0x7) << 2) | ((by[i + 1] & 0xC0) >> 6); // BBBBB
acPart[1] = ch_base32_map[char_index];
char_index = (by[i + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = ch_base32_map[char_index];
char_index = ((by[i + 1] & 0x1) << 4) | ((by[i + 2] & 0xF0) >> 4);// DDDDD
acPart[3] = ch_base32_map[char_index];
char_index = (by[i + 2] & 0xF) << 1; // EEEE0
acPart[4] = ch_base32_map[char_index];
char_count = 5;
break; case 4:
char_index = (by[i] & 0xF8) >> 3; // AAAAA
acPart[0] = ch_base32_map[char_index];
char_index = ((by[i] & 0x7) << 2) | ((by[i + 1] & 0xC0) >> 6); // BBBBB
acPart[1] = ch_base32_map[char_index];
char_index = (by[i + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = ch_base32_map[char_index];
char_index = ((by[i + 1] & 0x1) << 4) | ((by[i + 2] & 0xF0) >> 4);// DDDDD
acPart[3] = ch_base32_map[char_index];
char_index = ((by[i + 2] & 0xF) << 1) | ((by[i + 3] & 0x80) >> 7);// EEEEE
acPart[4] = ch_base32_map[char_index];
char_index = (by[i + 3] & 0x7F) >> 2; // FFFFF
acPart[5] = ch_base32_map[char_index];
char_index = (by[i + 3] & 0x3) << 3; // GG000
acPart[6] = ch_base32_map[char_index];
char_count = 7;
break; default: // >= 5
char_index = (by[i] & 0xF8) >> 3; // AAAAA
acPart[0] = ch_base32_map[char_index];
char_index = ((by[i] & 0x7) << 2) | ((by[i + 1] & 0xC0) >> 6); // BBBBB
acPart[1] = ch_base32_map[char_index];
char_index = (by[i + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = ch_base32_map[char_index];
char_index = ((by[i + 1] & 0x1) << 4) | ((by[i + 2] & 0xF0) >> 4);// DDDDD
acPart[3] = ch_base32_map[char_index];
char_index = ((by[i + 2] & 0xF) << 1) | ((by[i + 3] & 0x80) >> 7);// EEEEE
acPart[4] = ch_base32_map[char_index];
char_index = (by[i + 3] & 0x7F) >> 2; // FFFFF
acPart[5] = ch_base32_map[char_index];
char_index = ((by[i + 3] & 0x3) << 3) | ((by[i + 4] & 0xE0) >> 5);// GGGGG
acPart[6] = ch_base32_map[char_index];
char_index = by[i + 4] & 0x1F; // HHHHH
acPart[7] = ch_base32_map[char_index];
char_count = 8;
break;
} output.append(acPart, 0, char_count);
} return output.toString();
}
public String decode(String sData) throws UnsupportedEncodingException {
int i = 0, dwLength = 0;
int[] char_index = null;
byte[] output = null;
char[] acInput = null; if (sData == null || sData == "" || sData.equals(""))
return null; acInput = sData.toCharArray();
if (acInput == null)
return null; try {
//dwLength = (int) (acInput.length / 8f * 5f) + 1;
dwLength = (int) (acInput.length / 8f * 5f);
output = new byte[dwLength];
char_index = new int[8];
} catch (Exception e) {
e.printStackTrace();
}
if (acInput == null)
return null; dwLength = 0;
for (i = 0; i < acInput.length; i += 8) {
switch (acInput.length - i) {
/* case 1:
char_index[0] = GetCharIndex(acInput[i]); output[dwLength] = (byte) (char_index[0] << 3);
break;*/ case 2:
char_index[0] = GetCharIndex(acInput[i]);
char_index[1] = GetCharIndex(acInput[i + 1]); output[dwLength] = (byte) ((char_index[0] << 3) | (char_index[1] >> 2));
//output[dwLength + 1] = (byte) ((char_index[1] & 0x3) << 6);
break; /* case 3:
char_index[0] = GetCharIndex(acInput[i]);
char_index[1] = GetCharIndex(acInput[i + 1]);
char_index[2] = GetCharIndex(acInput[i + 2]); output[dwLength] = (byte) ((char_index[0] << 3) | (char_index[1] >> 2));
output[dwLength + 1] = (byte) (((char_index[1] & 0x3) << 6) | (char_index[2] << 1));
break;*/ case 4:
char_index[0] = GetCharIndex(acInput[i]);
char_index[1] = GetCharIndex(acInput[i + 1]);
char_index[2] = GetCharIndex(acInput[i + 2]);
char_index[3] = GetCharIndex(acInput[i + 3]); output[dwLength] = (byte) ((char_index[0] << 3) | (char_index[1] >> 2));
output[dwLength + 1] = (byte) (((char_index[1] & 0x3) << 6)
| (char_index[2] << 1) | (char_index[3] >> 4));
//output[dwLength + 2] = (byte) ((char_index[3] & 0xF) << 4);
break; case 5:
char_index[0] = GetCharIndex(acInput[i]);
char_index[1] = GetCharIndex(acInput[i + 1]);
char_index[2] = GetCharIndex(acInput[i + 2]);
char_index[3] = GetCharIndex(acInput[i + 3]);
char_index[4] = GetCharIndex(acInput[i + 4]); output[dwLength] = (byte) (char_index[0] << 3 | char_index[1] >> 2);
output[dwLength + 1] = (byte) (((char_index[1] & 0x3) << 6)
| (char_index[2] << 1) | (char_index[3] >> 4));
output[dwLength + 2] = (byte) (((char_index[3] & 0xF) << 4) | (char_index[4] >> 1));
//output[dwLength + 3] = (byte) ((char_index[4] & 0x1) << 7);
break; /* case 6:
char_index[0] = GetCharIndex(acInput[i]);
char_index[1] = GetCharIndex(acInput[i + 1]);
char_index[2] = GetCharIndex(acInput[i + 2]);
char_index[3] = GetCharIndex(acInput[i + 3]);
char_index[4] = GetCharIndex(acInput[i + 4]);
char_index[5] = GetCharIndex(acInput[i + 5]); output[dwLength] = (byte) ((char_index[0] << 3) | (char_index[1] >> 2));
output[dwLength + 1] = (byte) (((char_index[1] & 0x3) << 6)
| (char_index[2] << 1) | (char_index[3] >> 4));
output[dwLength + 2] = (byte) (((char_index[3] & 0xF) << 4) | (char_index[4] >> 1));
output[dwLength + 3] = (byte) (((char_index[4] & 0x1) << 7) | (char_index[5] << 2));
break;*/ case 7:
char_index[0] = GetCharIndex(acInput[i]);
char_index[1] = GetCharIndex(acInput[i + 1]);
char_index[2] = GetCharIndex(acInput[i + 2]);
char_index[3] = GetCharIndex(acInput[i + 3]);
char_index[4] = GetCharIndex(acInput[i + 4]);
char_index[5] = GetCharIndex(acInput[i + 5]);
char_index[6] = GetCharIndex(acInput[i + 6]); output[dwLength] = (byte) ((char_index[0] << 3) | (char_index[1] >> 2));
output[dwLength + 1] = (byte) (((char_index[1] & 0x3) << 6)
| (char_index[2] << 1) | (char_index[3] >> 4));
output[dwLength + 2] = (byte) (((char_index[3] & 0xF) << 4) | (char_index[4] >> 1));
output[dwLength + 3] = (byte) (((char_index[4] & 0x1) << 7)
| (char_index[5] << 2) | (char_index[6] >> 3));
//output[dwLength + 4] = (byte) ((char_index[6] & 0x7) << 5);
break; default:
char_index[0] = GetCharIndex(acInput[i]);
char_index[1] = GetCharIndex(acInput[i + 1]);
char_index[2] = GetCharIndex(acInput[i + 2]);
char_index[3] = GetCharIndex(acInput[i + 3]);
char_index[4] = GetCharIndex(acInput[i + 4]);
char_index[5] = GetCharIndex(acInput[i + 5]);
char_index[6] = GetCharIndex(acInput[i + 6]);
char_index[7] = GetCharIndex(acInput[i + 7]); output[dwLength] = (byte) ((char_index[0] << 3) | (char_index[1] >> 2));
output[dwLength + 1] = (byte) (((char_index[1] & 0x3) << 6)
| (char_index[2] << 1) | (char_index[3] >> 4));
output[dwLength + 2] = (byte) (((char_index[3] & 0xF) << 4) | (char_index[4] >> 1));
output[dwLength + 3] = (byte) (((char_index[4] & 0x1) << 7)
| (char_index[5] << 2) | (char_index[6] >> 3));
output[dwLength + 4] = (byte) (((char_index[6] & 0x7) << 5) | (char_index[7]));
break;
}
dwLength += 5;
}
String s = new String(output, "UTF-8");
return s;
}
private int base32_map_length = 32; private char[] ch_base32_map = { 'A', 'V', 'F', '8', 'E', 'C', 'R', 'H', // 0-7
'I', 'T', 'K', 'L', 'M', 'N', 'O', 'U', // 8-15
'Q', 'G', 'S', 'J', '4', 'B', 'W', 'X', // 16-23
'Y', 'Z', '9', 'D', '7', '6', '5', 'P', // 24-31
}; private int GetCharIndex(char ch) {
int i = 0;
for (; i < ch_base32_map.length; i++) {
if (ch == ch_base32_map[i])
break;
} return i;
} public static void main(String[] args) {
try {
Check2 check2 = new Check2(); String str_base = "结论是什么呢?结论就是:“在什么条件下," +
"中国能战胜并消灭日本帝国主义的实力呢?要有三个条件:" +
"第一是中国抗日统一战线的完成;" +
"第二是国际抗日统一战线的完成;" +
"第三是日本国内人民和日本殖民地人民的革命运动的兴起。";
String str = check2.encode(str_base);
System.out.println("编码后=" + str);
String str1 = check2.decode(str);
System.out.println("解码后=" + str1);
} catch (Exception e) {
e.printStackTrace();
}
}
}