软件采用Unicode编程,Base64 如何解码?

解决方案 »

  1.   

    Base64Encode Base64Decode
    可以参考我的博客 使用Atl Base64 对文件编解码 .
      

  2.   

    我就是照着一楼的做法,对一个文件进行解码
    PD94bWwgdmVyc2lvbj0iMS4wIiA/Pg0KPFZJUk5PU0lORk8+DQo8SE9TVElORk8gVVNFUklEPSIxMDAwMDAiIERFUFRJRD0iMTAwMDAwIiBVU0VSTkFNRT0iY2EwMS
    本该解为:
    <?xml version="1.0" ?>
    <VIRNOSINFO>
    <HOSTINFO USERID="100000" DEPTID="100000" USERNAME="ca01
    但解为了
    <?xml version="1.0" ?>
    <VIRNOSINFO>
    <HOSTINFO USERID="10"DUD擟?"U4U$?許?6
    有乱码。
      

  3.   

    又找了种方法,但是中言是乱码,代码如下:
    #include <string>
    //
    class Base64
    {
    public:    // convert from Base64 to ANSI
       CString encode(const CString in_str);    // convert from ANSI to Base64
        static CString decode(CString in_str);private:    // encode table
        const static std::string _base64_encode_chars;
     
        // decode table
        const static WCHAR _base64_decode_chars[128];
    };#endif  // _BASE64HELPER_H_
    #include "stdafx.h"
    #include "Base64.h"
    //
    const std::string Base64::_base64_encode_chars = 
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";const WCHAR Base64::_base64_decode_chars[] = 
    {
        -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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
        -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
        -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
    };CString Base64::encode(CString in_str)
    {
        CString out_str;
        WCHAR c1, c2, c3;
        int i = 0;
        int len = in_str.GetLength();    while ( i<len )
        {
            // read the first byte
            c1 = in_str[i++];
            if ( i==len )       // pad with "="
            {
                out_str += _base64_encode_chars[ c1>>2 ];
                out_str += _base64_encode_chars[ (c1&0x3)<<4 ];
                out_str += "==";
                break;
            }        // read the second byte
            c2 = in_str[i++];
            if ( i==len )       // pad with "="
            {
                out_str += _base64_encode_chars[ c1>>2 ];
                out_str += _base64_encode_chars[ ((c1&0x3)<<4) | ((c2&0xF0)>>4) ];
                out_str += _base64_encode_chars[ (c2&0xF)<<2 ];
                out_str += "=";
                break;
            }        // read the third byte
            c3 = in_str[i++];
            // convert into four bytes string
            out_str += _base64_encode_chars[ c1>>2 ];
            out_str += _base64_encode_chars[ ((c1&0x3)<<4) | ((c2&0xF0)>>4) ];
            out_str += _base64_encode_chars[ ((c2&0xF)<<2) | ((c3&0xC0)>>6) ];
            out_str += _base64_encode_chars[ c3&0x3F ];
        }    return out_str;
    }CString Base64::decode(CString in_str)
    {
        CString out_str;
        WCHAR c1, c2, c3, c4;
        int i = 0;
        int len = in_str.GetLength();    while ( i<len)
        {
            // read the first byte
            do {
                c1 = _base64_decode_chars[ in_str[i++] ];
            } while ( i<len && c1==-1);        if ( c1==-1)
                break;        // read the second byte
            do {
                c2 = _base64_decode_chars[ in_str[i++] ];
            } while ( i<len && c2==-1);        if ( c2==-1 )
                break;        // assamble the first byte
            out_str += WCHAR( (c1<<2) | ((c2&0x30)>>4) );        // read the third byte
            do {
                c3 = in_str[i++];
                if ( c3==61 )       // meet with "=", break
                    return out_str;
                c3 = _base64_decode_chars[ c3 ];
            } while ( i<len && c3==-1);        if ( c3==-1 )
                break;        // assamble the second byte
            out_str += WCHAR( ((c2&0XF)<<4) | ((c3&0x3C)>>2) );        // read the fourth byte
            do {
                c4 = in_str[i++];
                if ( c4==61 )       // meet with "=", break
                    return out_str;
                c4 = _base64_decode_chars[ c4 ];
            } while ( i<len && c4==-1 );        if ( c4==-1 )
                break;        // assamble the third byte
            out_str += WCHAR( ((c3&0x03)<<6) | c4 );
        }    return out_str;
    }
      

  4.   

     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    好像是 ‘+*
      

  5.   

    我测试的没有问题啊
    #include <atlenc.h>
    void Test()
    {
      LPCSTR srcCode = "PD94bWwgdmVyc2lvbj0iMS4wIiA/Pg0KPFZJUk5PU0lORk"
        "8+DQo8SE9TVElORk8gVVNFUklEPSIxMDAwMDAiIERFUFRJRD0iMTAwMDAwIiBVU0VSTkFNRT0iY2EwMS";
      int nSrcLength = strlen(srcCode);
      int charBuffLength = Base64DecodeGetRequiredLength(nSrcLength);
      char *chDstBuff = new char[charBuffLength+1];
      memset(chDstBuff, 0, charBuffLength+1);
      Base64Decode(srcCode, nSrcLength, (BYTE*)chDstBuff, &charBuffLength);
      OutputDebugStringA("\r\n");
      OutputDebugStringA(chDstBuff);
      OutputDebugStringA("\r\n");
      delete chDstBuff;
    }
    调试输出
    <?xml version="1.0" ?>
    <VIRNOSINFO>
    <HOSTINFO USERID="100000" DEPTID="100000" USERNAME="ca01
      

  6.   

    一样的啊,解码出来都是二进制数据。//**************************
    // base64解码
    //**************************
    int
    Base64_Decode( TCHAR * input, unsigned char *output)
    {
    //解码表
    const TCHAR DecodeTable[] =
    {
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    62, // '+'
    0, 0, 0,
    63, // '/'
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
    0, 0, 0, 0, 0, 0, 0,
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
    13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'
    0, 0, 0, 0, 0, 0,
    26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
    39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'
    }; size_t inlength = _tcslen( input );
    //返回值
    unsigned char *p = output; int nValue;
    size_t i = 0;
    int outlength = 0; while( i < inlength )
    {
    if( 0x0d != *input &&
    0x0a != *input )
    {
    nValue = DecodeTable[*input++] << 18;
    nValue += DecodeTable[*input++] << 12;
    *p = ( (nValue & 0x00FF0000) >> 16 );
    p++; outlength++; if( '=' != *input )
    {
    nValue += ( DecodeTable[*input++] << 6 );
    *p = ( (nValue & 0x0000FF00) >> 8 );
    p++;
    outlength++; if ( '=' != *input )
    {
    nValue += DecodeTable[*input++];
    *p = ( nValue & 0x000000FF );
    p++; outlength++;
    }
    }
    i += 4;
    }
    else// 回车换行,跳过
    {
    input++;
    i++;
    }
    }
    // ansi模式下
    output[ outlength ] = 0;#ifdef _UNICODE
    output[ outlength + 1 ] = 0;
    #endif return outlength;
    }