什么叫 base64编码?原理是什么啊? 一种普遍用于Internet的数据传输的编码方式,使用标准ascii字符进行编码,由此可以解决不同字符集的问题.这只是简单的解释. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 byte[] bytes = strSource.getBytes(); int intUnitCount = ( bytes.length - 1 ) / 3 + 1; byte[] buf = new byte[intUnitCount * 4]; int l = bytes.length / 3; int srcindex = 0; int dstindex = 0; for (int i=0; i<l; i++) { int x = ((bytes[srcindex] & 0x00FF) << 16) | ((bytes[srcindex+1] & 0x00FF) << 8) | (bytes[srcindex+2] & 0x00FF); srcindex += 3; buf[dstindex++] = (byte)( ( x >> 18 ) & 0x3F ); buf[dstindex++] = (byte)( ( x >> 12 ) & 0x3F ); buf[dstindex++] = (byte)( ( x >> 6 ) & 0x3F ); buf[dstindex++] = (byte)( x & 0x3F ); } if ( srcindex + 2 == bytes.length ) { int x = ( ( bytes[srcindex] & 0x00FF ) << 16 ) | ( ( bytes[srcindex+1] & 0x00FF ) << 8 ); buf[dstindex++] = (byte)( ( x >> 18 ) & 0x3F ); buf[dstindex++] = (byte)( ( x >> 12 ) & 0x3F ); buf[dstindex++] = (byte)( ( x >> 6 ) & 0x3F ); buf[dstindex++] = (byte)B64_MAPPING['=']; } else if ( srcindex + 1 == bytes.length ) { int x = ( bytes[srcindex] & 0x00FF ) << 16; buf[dstindex++] = (byte)( ( x >> 18 ) & 0x3F ); buf[dstindex++] = (byte)( ( x >> 12 ) & 0x3F ); buf[dstindex++] = (byte)B64_MAPPING['=']; buf[dstindex++] = (byte)B64_MAPPING['=']; } StringBuffer dst = new StringBuffer(); for (int i=0; i<dstindex; i++) { if ( ( i % 76 ) == 0 ) dst.append("\n"); dst.append( B64_ALPHABET.charAt( buf[i] ) ); } return dst.toString();you need to define B64_MAPPING, which is a byte[]. I suggest you not to go in too deep. 很抱歉,没有什么深入的研究,但是你可以从网上下载Base64的java源代码,这并不是难事,google上一找一大堆. 传输的二进制是8位一字节,而ASCII是7位为一字节。如果直接把二进制作为ASCII来显示会损失,所以,BASE64就是解决这个问题的。三个字节变为4个字符。网上的源码多了. 这两篇文章可以保证你明白http://www.luocong.com/articles/show_article.asp?Article_ID=17http://www.ietf.org/rfc/rfc2045.txt 帮忙看下这个程序 为什么没实现comparable的功能! String a = "haha"中的"haha"和a到底在堆中还是栈中还是其他区域,达人们都来讨论下。 请高手帮忙。。。。 我的IE不能显示applet小程序里的图形 初学JAVA有一个问题很不能理解,望高手解答 JAVA可以对文件进行操作吗 illeagl start expression 错误 关于 jbuilder 的小问题? 很郁闷的问题,但很简单--搞定后立刻结贴 关于位操作的问题 学java看什么书好? 请问处理
int intUnitCount = ( bytes.length - 1 ) / 3 + 1;
byte[] buf = new byte[intUnitCount * 4]; int l = bytes.length / 3;
int srcindex = 0;
int dstindex = 0;
for (int i=0; i<l; i++)
{
int x = ((bytes[srcindex] & 0x00FF) << 16) |
((bytes[srcindex+1] & 0x00FF) << 8) |
(bytes[srcindex+2] & 0x00FF);
srcindex += 3; buf[dstindex++] = (byte)( ( x >> 18 ) & 0x3F );
buf[dstindex++] = (byte)( ( x >> 12 ) & 0x3F );
buf[dstindex++] = (byte)( ( x >> 6 ) & 0x3F );
buf[dstindex++] = (byte)( x & 0x3F );
} if ( srcindex + 2 == bytes.length )
{
int x = ( ( bytes[srcindex] & 0x00FF ) << 16 ) |
( ( bytes[srcindex+1] & 0x00FF ) << 8 ); buf[dstindex++] = (byte)( ( x >> 18 ) & 0x3F );
buf[dstindex++] = (byte)( ( x >> 12 ) & 0x3F );
buf[dstindex++] = (byte)( ( x >> 6 ) & 0x3F );
buf[dstindex++] = (byte)B64_MAPPING['='];
}
else if ( srcindex + 1 == bytes.length )
{
int x = ( bytes[srcindex] & 0x00FF ) << 16; buf[dstindex++] = (byte)( ( x >> 18 ) & 0x3F );
buf[dstindex++] = (byte)( ( x >> 12 ) & 0x3F );
buf[dstindex++] = (byte)B64_MAPPING['='];
buf[dstindex++] = (byte)B64_MAPPING['='];
} StringBuffer dst = new StringBuffer();
for (int i=0; i<dstindex; i++)
{
if ( ( i % 76 ) == 0 )
dst.append("\n");
dst.append( B64_ALPHABET.charAt( buf[i] ) );
}
return dst.toString();
you need to define B64_MAPPING, which is a byte[]. I suggest you not to go in too deep.
http://www.ietf.org/rfc/rfc2045.txt