自己写的Base64编码

自己写的Base64编码函数非常蹩脚,有待改进。

发现有一个错误。不能拿着用

/**
 * Base64转换用到了Java的位运算
 * & , |, >>, <<, >>>
 * http://zh.wikipedia.org/wiki/Base64
 */
public class Base64Demo{
	//Base64查询表
public static final char[] encodeLine={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X',
	'Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3',
	'4','5','6','7','8','9','+','/'};
	public static void main(String[] args){
		String name = "shao";
		String girl = "xinh";
		
		
		Base64Demo be = new Base64Demo();
		System.out.println(be.encode(girl.getBytes()));
		//利用一个大牛写的类来验证是否是正确的Base64编码
		//BASE64Encoder encoder = new BASE64Encoder();
		//System.out.println(encoder.encode(girl.getBytes()));
		
	}
	/*Base64编码*/
	protected String encode(byte[] buf){
		StringBuilder encodeResult = new StringBuilder();
		byte op;		//用来提取bit位
		byte oe;		//用来提取bit位
		
		//第1个字符分离	
		int oneh=0xfc;//11111100B
		int onel=0x03;//00000011B
		//第2个字符分离
		int twoh=0xf0;//11110000B
		int twol=0x0f;//00001111B
		//第3个字符分离
		int threeh=0xc0;//11000000B
		int threel=0x3f;//00111111B
		
		int count = buf.length / 3;
		
		
		for(int j=0;j<count;j++){
			for(int i=0;i<4;i++){
				if((j * 3+2) > buf.length){break;}
				switch(i){
				case 0:
					//第一组
					op = buf[j *3 + 0];
					op &= oneh;//01111000B
					//System.out.println(encodeLine[op>>>2]);
					encodeResult.append(encodeLine[op>>>2]);
				break;
				case 1:
					//第二组
					op = buf[j *3 + 0];
					oe = buf[j *3 + 1];
					op &= onel;
					oe &= twoh;
					op <<= 4;
					oe >>>= 4;
					//System.out.println(encodeLine[op | oe]);
					encodeResult.append(encodeLine[op | oe]);
				break;
				case 2:
					//第三组
					oe = buf[j *3 + 1];
					op = buf[j *3 + 2];
					//2字节低四位3自己接高二位
					oe &= twol;
					oe <<= 2;	//左移2位
					op &= threeh;
					op >>>=6;	//右移6位
					//System.out.println(encodeLine[oe | op]);	
					encodeResult.append(encodeLine[oe | op]);
				break;
				case 3:
					//第四组
					op = buf[j *3 + 2];
					op &= threel;
					//System.out.println(encodeLine[op]);	
					encodeResult.append(encodeLine[op]);
				break;
				}
				
			}
			
		}
		//字节数不是3的倍数的情况
		if(buf.length % 3 != 0){
			int k = buf.length % 3;
			if(k==1){
				op = buf[buf.length-1];
				op &= oneh;
				//System.out.println(encodeLine[op>>>2]);
				encodeResult.append(encodeLine[op>>>2]); 
				op = buf[buf.length-1];
				op &= onel;
				encodeResult.append(encodeLine[op << 4]);
				encodeResult.append("==");
			}else if(k==2){
				op = buf[buf.length-2];
				oe = buf[buf.length-1];
				op &= oneh;
				encodeResult.append(encodeLine[op>>>2]); 

				op = buf[buf.length-2]; op &= onel;
				oe &= twoh;
				op <<= 4;
				oe >>>= 4;
				encodeResult.append(encodeLine[op | oe]);

				oe = buf[buf.length-1];
				oe &= twol;
				encodeResult.append(encodeLine[oe<<2]);
				encodeResult.append("=");
			}
			
		}
		return encodeResult.toString();
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值