将base64字符串转换成BYTE[]

Gordon0315 2013-01-31 03:06:24
现有一个base64字符串,由于实际需要,需将其转换为BYTE[]数组,在网上看了下相关的转换方法,但是都无法实现,请教高手帮忙 ,给出相关代码是最好的,谢了。
...全文
1743 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wlccomeon 2015-04-01
  • 打赏
  • 举报
回复
呃,,有没有Java版的~~
Gordon0315 2013-01-31
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
引用 1 楼 rocktyt2 的回复:昨天还是前天有个一样的帖子,坐等老赵来套用回帖模板 你绝不会白等: C/C++ code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646……
不管是不是套用模板 我觉得赵哥还是很威武的 学习了 谢谢
Gordon0315 2013-01-31
  • 打赏
  • 举报
回复
引用 4 楼 rocktyt2 的回复:
C/C++ code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061const char _base64_encode_chars[] = "ABCDEFGHIJKLMNOPQRSTUV……
谢谢您的分享 已解决
赵4老师 2013-01-31
  • 打赏
  • 举报
回复
引用 1 楼 rocktyt2 的回复:
昨天还是前天有个一样的帖子,坐等老赵来套用回帖模板
你绝不会白等:
#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;
}
rocktyt 2013-01-31
  • 打赏
  • 举报
回复
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()一下
Gordon0315 2013-01-31
  • 打赏
  • 举报
回复
引用 2 楼 xiaohuh421 的回复:
引用 楼主 bingao0315 的回复:现有一个base64字符串,由于实际需要,需将其转换为BYTE[]数组,在网上看了下相关的转换方法,但是都无法实现,请教高手帮忙 ,给出相关代码是最好的,谢了。 base64字符串 byte[] ……
需要转成ASCII码的
xiaohuh421 2013-01-31
  • 打赏
  • 举报
回复
引用 楼主 bingao0315 的回复:
现有一个base64字符串,由于实际需要,需将其转换为BYTE[]数组,在网上看了下相关的转换方法,但是都无法实现,请教高手帮忙 ,给出相关代码是最好的,谢了。 base64字符串 byte[] 格式转换 ……
如果你不考虑编码转换, 即不把base64转换成ASCII码什么的, 仅仅是想以BYTE类型来访问的话, 可以直接使用强制转换即可. base64 arry[100]; BYTE *pData = arry; pData即可当成一个BYTE数组来使用了. pData[12] = 12;
rocktyt 2013-01-31
  • 打赏
  • 举报
回复
昨天还是前天有个一样的帖子,坐等老赵来套用回帖模板

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧