Base64是一种基于64个可打印字符来表示二进制数据的表示方法。由于2的6次方等于64,所以每6个比特为一个单元,对应某个可打印字符。三个字节有24个比特,对应于4个Base64单元,即3个字节需要用4个可打印字符来表示。它可用来作为电子邮件的传输编码。在Base64中的可打印字符包括字母A-Z、a-z、数字0-9 ,这样共有62个字符,此外两个可打印符号在不同的系统中而不同。一些如uuencode的其他编码方法,和之后binhex的版本使用不同的64字符集来代表6个二进制数字,但是它们不叫Base64。
Base64常用于在通常处理文本数据的场合,表示、传输、存储一些二进制数据。包括MIME的email,email via MIME, 在XML中存储复杂数据.
public class Base64Util {
private static final int ENCODE_BLOCK=4;
private static final int UNENCODE_BLOCK=3;
private static final byte DEFAULTF_PAD='=';
private static final byte[] ENCODE_TABLE={
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98,
99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
109, 110, 111, 112, 113, 114, 115, 116, 117, 118,
119, 120, 121, 122, 65, 66, 67, 68, 69, 70, 71, 72,
73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
86, 87, 88, 89, 90, 33, 45
};
private static final byte DECODE_TABLE[]={
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,-1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 63, -1, -1, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 36, 37, 38, 39, 40, 41,
42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1,
-1, -1, -1, -1
};
public static String encode(String str){
if(str==null||str.length()==0) return null;
try {
return encode(str.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
public static String encode(byte[] b) throws UnsupportedEncodingException{
if(b==null||b.length==0) return null;
int modulus = b.length%3;
int len = getEncodeLength(b);
byte bytes[] = new byte[len];
int loop= b.length - modulus;
int b1,b2,b3;
for(int i=0,j=0;i<loop;){
b1 = b[i++] & 255;//0xFF
b2 = b[i++] & 255;
b3 = b[i++] & 255;
bytes[j++] = ENCODE_TABLE[(b1 >>> 2) & 63];
bytes[j++] = ENCODE_TABLE[((b1 << 4) | (b2 >>> 4)) & 0x3f];
bytes[j++] = ENCODE_TABLE[((b2 << 2) | (b3 >>> 6)) & 0x3f];
bytes[j++] = ENCODE_TABLE[b3 & 0x3f];
}
if(modulus == 1){
b1 = b[b.length-1] & 0xff;
bytes[bytes.length-4] = ENCODE_TABLE[(b1 >>> 2) & 0x3f];
bytes[bytes.length-3] = ENCODE_TABLE[(b1 << 4) & 0x3f];
bytes[bytes.length-2] = DEFAULTF_PAD;
bytes[bytes.length-1] = DEFAULTF_PAD;
}
if(modulus == 2){
b1=b[b.length-2] & 0xff;
b2=b[b.length-1] & 0xff;
bytes[bytes.length-4] = ENCODE_TABLE[(b1 >>> 2) & 0x3f];
bytes[bytes.length-3] = ENCODE_TABLE[((b1 << 4) | (b2 >> 4)) & 0x3f];
bytes[bytes.length-2] = ENCODE_TABLE[(b2 << 2) & 0x3f];
bytes[bytes.length-1] = DEFAULTF_PAD;
}
return new String(bytes,"UTF-8");
}
public static String decode(String str) throws UnsupportedEncodingException{
if(str==null||str.length()==0) return str;
return decode(str.getBytes("UTF-8"));
}
public static String decode(byte[] b) throws UnsupportedEncodingException{
byte[] bytes;
int b1, b2, b3, b4;
if(b == null || b.length == 0) return "";
int len=b.length;
int resultLength=getDecodeLength(b);
bytes=new byte[resultLength];
for(int i = 0, j = 0;i < len-4;){
b1 = DECODE_TABLE [b[i++]];
b2 = DECODE_TABLE [b[i++]];
b3 = DECODE_TABLE [b[i++]];
b4 = DECODE_TABLE [b[i++]];
bytes[j++] = (byte)((b1 << 2) | (b2 >> 4));
bytes[j++] = (byte)((b2 << 4) | (b3 >> 2));
bytes[j++] = (byte)((b3 << 6) | b4);
}
if( b[len-2] == DEFAULTF_PAD){//means modulus=1
b1 = DECODE_TABLE [b [len - 4]];
b2 = DECODE_TABLE [b [len - 3]];
bytes[resultLength - 1] = (byte)((b1 << 2) | (b2 >> 4));
}else if(b[len-1] == DEFAULTF_PAD){//means modulus=2
b1 = DECODE_TABLE [b [len - 4]];
b2 = DECODE_TABLE [b [len - 3]];
b3 = DECODE_TABLE [b [len - 2]];
bytes[resultLength - 2]=(byte)((b1 << 2) | (b2 >> 4));
bytes[resultLength - 1]=(byte)((b2 << 4) | (b3 >> 2));
}else{
b1 = DECODE_TABLE [b[len - 4]];
b2 = DECODE_TABLE [b[len - 3]];
b3 = DECODE_TABLE [b[len - 2]];
b4 = DECODE_TABLE [b[len - 1]];
bytes[resultLength - 3] = (byte)((b1 << 2) | (b2 >> 4));
bytes[resultLength - 2] = (byte)((b2 << 4) | (b3 >> 2));
bytes[resultLength - 1] = (byte)((b3 << 6) | b4);
}
return new String(bytes,"UTF-8");
}
private static int getDecodeLength(byte[] b){
int len=b.length;
if(b[len-2] == DEFAULTF_PAD){//two '=',means modulus=1
return (len / ENCODE_BLOCK) * UNENCODE_BLOCK - 2;
}else if(b[len-1] == '='){
return (len / ENCODE_BLOCK) * UNENCODE_BLOCK - 1;
}else{
return (len / ENCODE_BLOCK) * UNENCODE_BLOCK;
}
}
private static int getEncodeLength(byte[] b){
int len=b.length;
if(len==0) return 0;
return (((len+UNENCODE_BLOCK)-1)/UNENCODE_BLOCK)*ENCODE_BLOCK;
}
public static String base642Str(String str) throws UnsupportedEncodingException{
return new String(Base64.decodeBase64(str.getBytes("UTF-8")),"UTF-8");
}
public static void main(String str[]) throws UnsupportedEncodingException{
String testStr="测试base64位";
String encodeStr=Base64Util.encode(testStr);
System.out.println(encodeStr);
String decodeStr=Base64Util.decode(encodeStr);
System.out.println(decodeStr);
}
}
分享到:
相关推荐
用c++实现的九宫重排问题 能在vc++6.0下运行 适合于初学c++语言的同学 形象的展示了九宫重排问题
基于FPGA的TCP段乱序重排设计与实现.pdf
重排链表 重排链表是指将一个链表重新排列,使得链表的第一个节点和最后一个节点相邻,第二个节点和倒数第二个节点相邻,以此类推。
重排九宫的广度优先算法 有代价函数实现局部优先 用哈希表来看是否已经在open表中
这是通过MATLAB平台的重排时频分析实现
重排链表.md
简单易懂,一列火车n节车厢,任意顺序入轨,通过有限个缓冲轨,按1—n顺序出轨。用于数据结构实验的参考
pandas重排数据表,透视图操作,样例程序,仅供参考
九宫重排
用C#实现的九宫格问题,包括界面的设计,游戏等级的设置,根据不同等级显示不同的初始化内容!
是一篇关于重排九宫这种经典益智游戏的学术论文
九宫重排Python 实现九宫重排的示例代码
实验要求设计一个队列结果来解决车厢重排问题。车厢重排问题为:一列货车共有n节车厢,每个车厢都有自己的编号,编号范围从1~n。给定任意次序的车厢,通过转轨站将车厢编号按顺序重新排成1~n。转轨站共有k个缓冲轨,...
代码实现了基本的功能,但是...设计并实现车厢重排算法; 分析算法的时间性能。 581742963 --------- H1 ----------- 987654321 --------- H2 ---------- 入 轨 --------- H3 ----------- 出 轨 图1 转轨站示意图
1、长按重排 2、跨cell重排
蓝桥杯历届试题解法:蓝桥杯竞赛中的九宫重排题目提供了多种解法,包括使用BFS结合集合进行判重和使用单向BFS结合集合进行判重等。这些解法都是围绕如何高效地找到从初始状态到目标状态的路径而设计的。
山东大学 数据结构课程设计 堆栈实现 火车厢重排单步演示软件
不同搜索算法解决九宫重排问题,JAVA语言实现广度优先,全局择优算法解九宫重排问题,输出路径。。。。。。
分别使用广度优先搜索、深度优先搜索和 A*算法实现重排九宫格问题; 使用的搜索方式可在程序内修改; 可以自行输入初始状态和目标状态 可以选择是否展示具体搜索步骤; 程序输出在该搜索方法下的最佳路径以及使用该...
一列货车共有n 节车厢,每个车厢都有...开始时,车厢从入轨进入缓冲轨,经过缓冲轨的重排后,按1~n 的顺序进入出轨。 缓冲轨按照先进先出方式,编写一个算法,将任意次序的车厢进行重排,输出每个缓冲轨 中的车厢编号。