一种普遍用于Internet的数据传输的编码方式,使用标准ascii字符进行编码,由此可以解决不同字符集的问题.这只是简单的解释.

解决方案 »

  1.   

    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.
      

  2.   

    很抱歉,没有什么深入的研究,但是你可以从网上下载Base64的java源代码,这并不是难事,google上一找一大堆.
      

  3.   

    传输的二进制是8位一字节,而ASCII是7位为一字节。如果直接把二进制作为ASCII来显示会损失,所以,BASE64就是解决这个问题的。三个字节变为4个字符。网上的源码多了.
      

  4.   

    这两篇文章可以保证你明白http://www.luocong.com/articles/show_article.asp?Article_ID=17
    http://www.ietf.org/rfc/rfc2045.txt