首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

将base64字符串转换成BYTE[]解决方案

2013-02-18 
将base64字符串转换成BYTE[]现有一个base64字符串,由于实际需要,需将其转换为BYTE[]数组,在网上看了下相关

将base64字符串转换成BYTE[]
现有一个base64字符串,由于实际需要,需将其转换为BYTE[]数组,在网上看了下相关的转换方法,但是都无法实现,请教高手帮忙 ,给出相关代码是最好的,谢了。 base64字符串 byte[] 格式转换
[解决办法]

const char _base64_encode_chars[] = 
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string decode(const std::string in_str)
{
    std::string out_str;
    char c1, c2, c3, c4;
    int i = 0;
    int len = in_str.length();

    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 += char( (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 += char( ((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 += char( ((c3&0x03)<<6) 
[解决办法]
 c4 );
    }

    return out_str;
}

传入和传出的参数类型自己转换下吧,比如返回值是str,要转成BYTE[]就(const BYTE*)str.c_str()一下
[解决办法]
引用:
昨天还是前天有个一样的帖子,坐等老赵来套用回帖模板

你绝不会白等:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BASE64_VALUE_SZ 256
int     base64_value[BASE64_VALUE_SZ];
const unsigned char alphabet[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
class Base64Utility {
public:
    Base64Utility();
    int base64_encode(char *src, int srclen, char *dst, int tail);
    int base64_decode(char *src, int srclen, char *dst);
private:
    void base_64_init(void);
};
Base64Utility::Base64Utility() {
    base_64_init();
}
void Base64Utility::base_64_init(void) {
    int i;

    for (i = 0; i < BASE64_VALUE_SZ; i++) base64_value[i] = -1;
    for (i = 0; i < 64; i++) base64_value[(int) alphabet[i]] = i;
    base64_value['='] = 0;
}
int Base64Utility::base64_encode(char *src, int srclen, char *dst, int tail) {
    int     bits, char_count, len;
    char    *o_char, *lim, *o_lim;
    unsigned char   c;

    if ( !src 
[解决办法]
 !dst) return 0;
    len = srclen;
    lim = src + len;
    o_char = dst;
    o_lim  = dst + (len*4)/3 + 1;
    char_count = 0;
    bits = 0;
    while ( (src < lim) && (o_char < o_lim)) {
        c = *(src++);
        bits += c;


        char_count++;
        if (char_count == 3) {
            *(o_char++) = alphabet[bits >> 18];
            *(o_char++) = alphabet[(bits >> 12) & 0x3f];
            *(o_char++) = alphabet[(bits >> 6) & 0x3f];
            *(o_char++) = alphabet[bits & 0x3f];
            bits = 0;
            char_count = 0;
        } else {
            bits <<= 8;
        }
    }
    if (char_count != 0) {
        bits <<= 16 - (8 * char_count);
        *(o_char++) = alphabet[bits >> 18];
        *(o_char++) = alphabet[(bits >> 12) & 0x3f];
        if (char_count == 1) {
            if (tail) {
                *(o_char++) = '=';
                *(o_char++) = '=';
            }
        } else {
            *(o_char++) = alphabet[(bits >> 6) & 0x3f];
            if (tail) {
                *(o_char++) = '=';
            }
        }
    }
    *(o_char) = 0;
    return strlen(dst);
}
int Base64Utility::base64_decode(char *src, int srclen, char *dst) {
    int j;
    unsigned int k;
    int c, base_result_sz;
    long val;

    if (!src 
[解决办法]
 !dst) return 0;
    base_result_sz = srclen;
    val = c = 0;
    for (j = 0; *src; src++) {
        k = (int) *src % BASE64_VALUE_SZ;
        if (base64_value[k] < 0) continue;
        val <<= 6;
        val += base64_value[k];
        if (++c < 4) continue;


        dst[j++] = (char) (val >> 16);
        dst[j++] = (val >> 8) & 0xff;
        dst[j++] = val & 0xff;
        val = c = 0;
    }
    switch (c) {
        case 2://xxxxxx xx0000
            dst[j++] = (val >> 4) & 0xff;
        break;
        case 3://XXXXXX XXxxxx xxxx00
            dst[j++] = (char) (val >> 10);
            dst[j++] = (val >> 2) & 0xff;
        break;
    }
    return j;
}
Base64Utility b64u;
#define MAXLENS 1024768
#define MAXLEND 1366360
char bufd[MAXLEND];
char bufs[MAXLENS];
FILE *fs,*fd;
int fsize;
int main(int argc,char *argv[]) {
    if (argc<4) {
    USE:
        printf("%s <-e
[解决办法]
-E
[解决办法]
-d> srcfile desfile\n",argv[0]);
        return 1;
    }
    if (stricmp(argv[1],"-e") && stricmp(argv[1],"-d")) goto USE;
    if (0==stricmp(argv[1],"-e")) {
        fs=fopen(argv[2],"rb");
        if (NULL==fs) {
            printf("Can not open file %s!\n",argv[2]);
            return 2;
        }
        fsize=fread(bufs,1,MAXLENS,fs);
        if (fsize<=0) {
            fclose(fs);
            printf("Can not read file %s!\n",argv[2]);
            return 3;
        }
        if (MAXLENS==fsize) printf("Warning: Up to %d bytes.\n",MAXLENS);
        fclose(fs);
        b64u.base64_encode(bufs,fsize,bufd,('E'==argv[2][1]));
        fd=fopen(argv[3],"w");
        if (NULL==fd) {
            printf("Can not create file %s!\n",argv[3]);
            return 4;
        }


        fprintf(fd,"%s",bufd);
        fclose(fd);
    } else {//0==stricmp(argv[1],"-d")
        fd=fopen(argv[2],"rb");
        if (NULL==fd) {
            printf("Can not open file %s!\n",argv[2]);
            return 2;
        }
        fsize=fread(bufd,1,MAXLEND,fd);
        if (fsize<=0) {
            fclose(fd);
            printf("Can not read file %s!\n",argv[2]);
            return 3;
        }
        if (MAXLEND==fsize) printf("Warning: Up to %d bytes.\n",MAXLEND);
        fclose(fd);
        fsize=b64u.base64_decode(bufd,fsize,bufs);
        fs=fopen(argv[3],"wb");
        if (NULL==fs) {
            printf("Can not create file %s!\n",argv[3]);
            return 4;
        }
        if (fsize!=(int)fwrite(bufs,1,fsize,fs)) {
            printf("Write %s error!\n",argv[3]);
            fclose(fs);
            return 5;
        }
        fclose(fs);
    }
    return 0;
}


热点排行