C++/MFC Base64 codec base64 encoding base64 decoding

Source: Internet
Author: User
Tags base64

unsigned char * base64= (unsigned char *) "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/="; CString Base64Encode (CString src,int srclen) {int n,buflen,i,j; static unsigned char *dst; CString buf=src; Buflen=n=srclen; dst= (unsigned char*) malloc (buflen/3*4+3); memset (dst,0,buflen/3*4+3); for (i=0,j=0;i<=buflen-3;i+=3,j+=4) {dst[j]= (BUF[I]&AMP;0XFC) >>2; dst[j+1]= ((buf[i]&0x03) <<4) + ((buf[i+1]&0xf0) >>4); Dst[j+2]= ((buf[i+1]&0x0f) <<2) + ((buf[i+2]&0xc0) >>6); dst[j+3]=buf[i+2]&0x3f; if (n%3==1) {dst[j]= (BUF[I]&AMP;0XFC) >>2; dst[j+1]= ((buf[i]&0x03) <<4); dst[j+2]=64; dst[j+3]=64 = 4; else if (n%3==2) {dst[j]= (BUF[I]&AMP;0XFC) >>2; dst[j+1]= ((buf[i]&0x03) <<4) + ((buf[i+1]&0xf0) &GT;&GT;4); Dst[j+2]= ((buf[i+1]&0x0f) <<2); dst[j+3]=64; j+=4; for (i=0;i<j;i++)/* Map 6 bit value to Base64 ASCII character/dst[i]=base64[(int) dst[i]]; dst[j]=0; Return CString (DST); } CString BAse64decode (CString inpt,int * len) {int n,i,j,pad; unsigned char *p; static unsigned char *dst; unsigned char * SRC; *le n=0; Pad=0; N=INPT. GetLength (); src=new unsigned char [n]; for (i=0;i<n;i++) src[i]=inpt[i]; while (n>0&&src[n-1]== ' = ') {src[n-1]=0; pad++ n--;} for (i=0;i<n;i++) {/* map base64 ASCII character to 6 Bit value */p= (unsigned char *) STRCHR (const char *) base64, (int) src[i]); if (!p) break; src[i]=p-(unsigned char *) base64; } dst= (unsigned char *) malloc (n*3/4+1); memset (dst,0,n*3/4+1); for (i=0,j=0;i<n;i+=4,j+=3) {dst[j]= (src[i]<<2) + ((src[i+1]&0x30) >>4); dst[j+1]= ((src[i+1]& 0x0f) (<<4) + ((src[i+2]&0x3c) >>2); Dst[j+2]= ((src[i+2]&0x03) <<6) + src[i+3]; *len+=3; } *len-=pad; Return CString (DST); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.