C Language Implementation Base64 encoding, Base64 decoding

Source: Internet
Author: User
Tags base64

Char *base64_encode (char *bindata, char *base64, int binlength) {         int i = 0;         int j = 0;        int current = 0;         for  (i = 0; i < binlength; i &NBSP;+=&NBSP;3)  {                 //get the first 6-bit                  current =  (* (bindata+i)  >> 2)  & 0x3F;                 * (base64 + j++)  =  base64char[current];                 //gets the first two bits of the second 6-bit &NBsp;               current =   (* (bindata+i)  << 4)  & 0x30;                 //If there is only one character, then special handling is required                  if  (binlength <=  (i+1))  {                          * (base64 + j++)  = base64char[current];                          * (base64 + j++)  =  ' = ';                         * (base64  + j++)  = ' = ';                         break;                 }                 //get the second 6-bit post four bit                  current |=  (* (bindata+i+1)  >> 4 )  & 0xf;                 * (base64  + j++)  = base64char[current];                 //get the third 6-bit top four                  current =  (* (bindata+i+1)  << 2 )  &  0x3c;                if  (binLength <=   (i+2)  {                         * (base64 + j++)  = base64char[current ];                         * (base64 + j++)  =  ' = ';                          break;                }                 //gets the third 6 bit of the second two digits                 current | =  (* (bindata+i+2)  >> 6)  & 0x03;                 * (base64 + j++)  = base64char[current];                 //get fourth 6-bit                  current = * ( BINDATA+I+2)  & 0x3F;                 * (base64 + j++)  = base64char[current];         }        * (base64+j)  =  ';  '        return base64;} Char *base64_decode (char const *base64str, char *debase64str, int  Encodestrlen) {        int i = 0;        int j = 0;         int k = 0;        char temp[4] =   "";        for  (i = 0; i <  ENCODESTRLEN;&NBSP;I&NBSP;+=&NBSP;4)  {                 for  (j = 0; j < 64 ; j++)  {                          if  (* (base64str + i)  == base64char[j])  {                                  temp[0] = j;                         }                 }                 for  (j = 0;  j < 64 ; j++)  {                         if  (* (base64str  + i + 1)  == base64char[j])  {                                  temp[1] = j;                         }                 }                 for  (j = 0; j < 64 ;  j++)  {                         if  (* (base64str + i + 2)  = =&NBSP;BASE64CHAR[J])  {                                  temp[2] = j;                         }                 }                 for  (j = 0; j < 64 ; j++)  {                          if  (* (base64str + i + 3)  == base64char[j])  {                                  temp[3] = j;                          }                 }                * (debase64str + k++)  =  ((temp[0] << 2) &NBSP;&AMP;&NBSP;0XFC)  |  (( TEMP[1]&GT;&GT;4)  &AMP;&NBSP;0X03);                 if  ( * (base64str + i + 2)   ==  ' = '  )                           break;                 * (debase64str + k++)  =  ((temp[1] << 4)  & 0xf0)  |  ((temp[2]>>2)  & 0x0f);                 if  ( * (base64str + i + 3)  ==   ' = '  )                          break;                 * (debase64str + k++)  =  ((temp[2] << 6)  & 0xf0 )  |  (temp[3] & 0x3f);        }         return debase64str;}


























C Language Implementation Base64 encoding, Base64 decoding

Related Article

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.