一个字符串用 base64编码的,但是解码的时候不支持中文,请问谁能够提供一个方法让他支持下中文啊。

解决方案 »

  1.   

    base64是6位的,好像标准还没同一。全部转成二进制,再转成char,再转成unicode?
      

  2.   

    base64是加密算法.不区分是不是中文,
    统一按二进制加密解密.
      

  3.   

    http://sourceforge.net/projects/base64/
      

  4.   

    base64编码是加密后的一种字符串格式的数据,你可以通过解密方式获取原文
      

  5.   

    vc6下在帮助中搜索“ToBase64”和“FromBase64”这2个函数,
    移植一下就可以用
      

  6.   


    HRESULT CSimpleLogObj::ToBase64(LPVOID pv, ULONG cbStart, BSTR* pbstr) 
    // 
    // Encode and return the bytes in base 64 
    // 
        { 
        ULONG cb = cbStart; 
        *pbstr = NULL; 
        HRESULT hr = S_OK; 
        int cchPerLine = 72;                        // conservative, must be mult of 4 for us 
        int cbPerLine  = cchPerLine / 4 * 3; 
        int cbSafe     = cb + 3;                    // allow for padding 
        int cLine      = cbSafe / cbPerLine + 2;    // conservative 
        int cchNeeded  = cLine * (cchPerLine + 2 /*CRLF*/) + 1 /*NULL*/; 
        int cbNeeded   = cchNeeded * sizeof(WCHAR); 
        LPWSTR wsz = (LPWSTR)CoTaskMemAlloc(cbNeeded); 
        if (wsz) 
            { 
            BYTE*  pb   = (BYTE*)pv; 
            WCHAR* pch  = wsz; 
            int cchLine = 0; 
            // 
            // Main encoding loop 
            // 
            while (cb >= 3) 
                { 
                BYTE b0 =                     ((pb[0]>>2) & 0x3F); 
                BYTE b1 = ((pb[0]&0x03)<<4) | ((pb[1]>>4) & 0x0F); 
                BYTE b2 = ((pb[1]&0x0F)<<2) | ((pb[2]>>6) & 0x03); 
                BYTE b3 = ((pb[2]&0x3F)); 
     
                *pch++ = rgwchBase64[b0]; 
                *pch++ = rgwchBase64[b1]; 
                *pch++ = rgwchBase64[b2]; 
                *pch++ = rgwchBase64[b3]; 
     
                pb += 3; 
                cb -= 3; 
                 
                // put in line breaks 
                cchLine += 4; 
                if (cchLine >= cchPerLine) 
                    { 
                    *pch++ = L'\r'; 
                    *pch++ = L'\n'; 
                    cchLine = 0; 
                    } 
                } 
            // 
            // Account for gunk at the end 
            // 
            *pch++ = L'\r';     // easier than keeping track 
            *pch++ = L'\n'; 
            if (cb==0) 
                { 
                // nothing to do 
                } 
            else if (cb==1) 
                { 
                BYTE b0 =                     ((pb[0]>>2) & 0x3F); 
                BYTE b1 = ((pb[0]&0x03)<<4) | 0; 
                *pch++ = rgwchBase64[b0]; 
                *pch++ = rgwchBase64[b1]; 
                *pch++ = L'='; 
                *pch++ = L'='; 
                } 
            else if (cb==2) 
                { 
                BYTE b0 =                     ((pb[0]>>2) & 0x3F); 
                BYTE b1 = ((pb[0]&0x03)<<4) | ((pb[1]>>4) & 0x0F); 
                BYTE b2 = ((pb[1]&0x0F)<<2) | 0; 
                *pch++ = rgwchBase64[b0]; 
                *pch++ = rgwchBase64[b1]; 
                *pch++ = rgwchBase64[b2]; 
                *pch++ = L'='; 
                } 
             
            // 
            // NULL terminate the string 
            // 
            *pch = NULL; 
     
            // 
            // Allocate our final output 
            // 
            *pbstr = SysAllocString(wsz); 
            if (*pbstr==NULL) 
                hr = E_OUTOFMEMORY; 
     
            CoTaskMemFree(wsz); 
            } 
        else 
            hr = E_OUTOFMEMORY; 
     
        #ifdef _DEBUG 
        if (hr==S_OK) 
            { 
            BLOB b; 
            FromBase64(*pbstr, &b); 
            _ASSERTE(b.cbSize == cbStart); 
            _ASSERTE(memcmp(b.pBlobData, pv, cbStart) == 0); 
            CoTaskMemFree(b.pBlobData); 
            } 
        #endif 
     
        return hr; 
        } 
      

  7.   


    HRESULT CSimpleLogObj::FromBase64(BSTR bstr, BLOB* pblob) 
    // 
    // Decode and return the Base64 encoded bytes 
    // 
        { 
        HRESULT hr = S_OK; 
        int cbNeeded = lstrlenW(bstr);      // an upper bound 
        BYTE* rgb = (BYTE*)CoTaskMemAlloc(cbNeeded); 
        if (rgb) 
            { 
            BYTE  mpwchb[256]; 
            BYTE  bBad = (BYTE)-1; 
            int   i; 
            // 
            // Initialize our decoding array 
            // 
            memset(&mpwchb[0], bBad, 256); 
            for (i = 0; i < 64; i++) 
                { 
                WCHAR wch = rgwchBase64[i]; 
                mpwchb[wch] = i; 
                } 
     
            // 
            // Loop over the entire input buffer 
            // 
            ULONG bCurrent = 0;         // what we're in the process of filling up 
            int  cbitFilled = 0;        // how many bits in it we've filled 
            BYTE* pb = rgb;             // current destination (not filled) 
            // 
            for (WCHAR* pwch=bstr; *pwch; pwch++) 
                { 
                WCHAR wch = *pwch; 
                // 
                // Ignore white space 
                // 
                if (wch==0x0A || wch==0x0D || wch==0x20 || wch==0x09) 
                    continue; 
                // 
                // Have we reached the end? 
                // 
                if (wch==L'=') 
                    break; 
                // 
                // How much is this character worth? 
                // 
                BYTE bDigit = mpwchb[wch]; 
                if (bDigit==bBad) 
                    { 
                    hr = E_INVALIDARG; 
                    break; 
                    } 
                // 
                // Add in its contribution 
                // 
                bCurrent <<= 6; 
                bCurrent |= bDigit; 
                cbitFilled += 6; 
                // 
                // If we've got enough, output a byte 
                // 
                if (cbitFilled >= 8) 
                    { 
                    ULONG b = (bCurrent >> (cbitFilled-8));     // get's top eight valid bits 
                    *pb++ = (BYTE)(b&0xFF);                     // store the byte away 
                    cbitFilled -= 8; 
                    } 
                } 
     
            if (hr==S_OK) 
                { 
                pblob->pBlobData = rgb; 
                pblob->cbSize = pb-rgb; 
                } 
            else 
                { 
                CoTaskMemFree(rgb); 
                pblob->pBlobData = NULL; 
                } 
            } 
        else 
            hr = E_OUTOFMEMORY; 
        return hr;