问题描述
1.实现一个lite版的字符串替换函数char[]strreplace(char[]str,char[]sub,char[]rep)2.对任意数据进行Base64编码char[]base64_encode(byte[]data)
解决方案
解决方案二:
下面基于Java实现的Base64的编码和解码程序:packagecom.gloomyfish.smtp.util;publicclassBase64Coder{publicfinalstaticchar[]base64_alphabet=newchar[]{'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','+','/','='};publicstaticStringencode(Stringcontent){byte[]data=content.getBytes();intlength=data.length;byte[]char_array_3=newbyte[]{0,0,0};byte[]char_array_4=newbyte[]{'=','=','=','='};StringretContent="";inti=0;intj=0;intreversePos=0;while(length>0){length--;char_array_3[i++]=data[reversePos++];if(i==3){char_array_4[0]=(byte)((char_array_3[0]&0xfc)>>2);//convertthecharchar_array_4[1]=(byte)(((char_array_3[0]&0x03)<<4)+((char_array_3[1]&0xf0)>>4));char_array_4[2]=(byte)(((char_array_3[1]&0x0f)<<2)+((char_array_3[2]&0xc0)>>6));char_array_4[3]=(byte)(char_array_3[2]&0x3f);for(i=0;(i<4);i++)retContent+=base64_alphabet[char_array_4[i]];i=0;}}//handlingthelastinputcontentif(i>0){for(j=i;j<3;j++)char_array_3[j]=0;//paddingofzerochar_array_4[0]=(byte)((char_array_3[0]&0xfc)>>2);//rightshiftchar_array_4[1]=(byte)(((char_array_3[0]&0x03)<<4)+((char_array_3[1]&0xf0)>>4));char_array_4[2]=(byte)(((char_array_3[1]&0x0f)<<2)+((char_array_3[2]&0xc0)>>6));char_array_4[3]=(byte)(char_array_3[2]&0x3f);for(j=0;(j<i+1);j++)retContent+=base64_alphabet[char_array_4[j]];while((i++<3))//paddingof'='ofoutputstringretContent+='=';}returnretContent;}publicstaticStringdecode(StringenContent){byte[]data=enContent.getBytes();inti=0,j=0,enCode=0;intmLength=data.length;byte[]char_array_4=newbyte[4];byte[]char_array_3=newbyte[3];StringretContent="";//filteroutthepadding'='charswhile(mLength>0&&(((char)data[enCode])!='=')&&isBase64((char)data[enCode])){mLength--;char_array_4[i++]=data[enCode++];if(i==4){for(i=0;i<4;i++)char_array_4[i]=findChar((char)char_array_4[i]);char_array_3[0]=(byte)((char_array_4[0]<<2)+((char_array_4[1]&0x30)>>4));char_array_3[1]=(byte)(((char_array_4[1]&0xf)<<4)+((char_array_4[2]&0x3c)>>2));char_array_3[2]=(byte)(((char_array_4[2]&0x3)<<6)+char_array_4[3]);for(i=0;(i<3);i++)retContent+=(char)char_array_3[i];i=0;}}//lastcontenthandlingif(i>0){for(j=i;j<4;j++)char_array_4[j]=0;for(j=0;j<4;j++)char_array_4[j]=findChar((char)char_array_4[j]);char_array_3[0]=(byte)((char_array_4[0]<<2)+((char_array_4[1]&0x30)>>4));char_array_3[1]=(byte)(((char_array_4[1]&0xf)<<4)+((char_array_4[2]&0x3c)>>2));char_array_3[2]=(byte)(((char_array_4[2]&0x3)<<6)+char_array_4[3]);for(j=0;(j<i-1);j++)retContent+=(char)char_array_3[j];}returnretContent;}publicstaticbooleanisBase64(charc){booleanbase64=false;for(inti=0;i<64;i++){if(c==base64_alphabet[i]){base64=true;break;}}returnbase64;}publicstaticbytefindChar(charx){byteindex=64;//65thchar'='for(inti=0;i<64;i++){if(x==base64_alphabet[i]){index=(byte)i;break;}}returnindex;}/***<p>testdataandresultshouldlikebelowoutput,RFC4648Sample</p>*BASE64("")=""*BASE64("f")="Zg=="*BASE64("fo")="Zm8="*BASE64("foo")="Zm9v"*BASE64("foob")="Zm9vYg=="*BASE64("fooba")="Zm9vYmE="*BASE64("foobar")="Zm9vYmFy"***@paramargs*/publicstaticvoidmain(String[]args){//BASE64Encodercoder=newBASE64Encoder();//System.out.println(coder.encode("foobar".getBytes()));System.out.println("#--------------encode---------------#");System.out.println(encode(""));System.out.println(encode("f"));System.out.println(encode("fo"));System.out.println(encode("foo"));System.out.println(encode("foob"));System.out.println(encode("fooba"));System.out.println(encode("foobar"));System.out.println(encode("123456789sS{1}quot;));System.out.println("#--------------decode---------------#");System.out.println(decode(""));System.out.println(decode("Zg=="));System.out.println(decode("Zm8="));System.out.println(decode("Zm9v"));System.out.println(decode("Zm9vYg=="));System.out.println(decode("Zm9vYmE="));System.out.println(decode("Zm9vYmFy"));System.out.println(decode("MTIzNDU2Nzg5c1Mk"));}}
解决方案三:
那第一道题那,我没读懂题的意思
解决方案四:
该回复于2014-09-22 08:43:38被版主删除