Base64 code in C Language

Source: Internet
Author: User

C does not contain strings. It must be implemented using char *.

# Include <stdio. h> # include "base. H "const char base [] =" character +/= "; char * base64_encode (const char * data, int data_len) {// int data_len = strlen (data ); int prepare = 0; int ret_len; int temp = 0; char * ret = NULL; char * f = NULL; int TMP = 0; char changed [4]; int I = 0; ret_len = data_len/3; temp = data_len % 3; if (temp> 0) {ret_len + = 1;} ret_len = ret_len * 4 + 1; ret = (char *) malloc (ret_len); If (ret = NULL) {printf ("no enough memory. \ n "); exit (0);} memset (Ret, 0, ret_len); F = ret; while (TMP <data_len) {temp = 0; prepare = 0; memset (changed, '\ 0', 4); While (temp <3) {// printf ("tmp = % d \ n", TMP ); if (TMP> = data_len) {break;} prepare = (prepare <8) | (data [TMP] & 0xff); TMP ++; temp ++ ;} prepare = (prepare <(3-temp) * 8); // printf ("before for: temp = % d, prepare = % d \ n", temp, prepare); for (I = 0; I <4; I ++) {If (temp <I) {changed [I] = 0x40 ;} else {changed [I] = (prepare> (3-I) * 6) & 0x3f;} * f = base [changed [I]; // printf ("%. 2x ", changed [I]); F ++;} * f = '\ 0'; return ret;}/**/static char find_pos (char ch) {char * PTR = (char *) strrchr (base, CH); // the last position (the only) in base [] Return (PTR-base );} /**/char * base64_decode (const char * data, int data_len) {int ret_len = (data_len/4) * 3; int counter _count = 0; char * ret = NULL; char * f = NULL; int TMP = 0; int temp = 0; char need [3]; int prepare = 0; int I = 0; if (* (Data + data_len-1) = ') {response _count + = 1;} If (* (Data + data_len-2) = ') {export _count + = 1;} If (* (Data + data_len-3) = ') {// seems impossible export _count + = 1;} switch (export _count) {Case 0: ret_len + = 4; // 3 + 1 [1 for null] Break; Case 1: ret_len + = 4; // Ceil (6*3) /8) + 1 break; Case 2: ret_len + = 3; // Ceil (6*2)/8) + 1 break; Case 3: ret_len + = 2; // Ceil (6*1)/8) + 1 break;} ret = (char *) malloc (ret_len); If (ret = NULL) {printf ("no enough memory. \ n "); exit (0);} memset (Ret, 0, ret_len); F = ret; while (TMP <(data_len-assist_count) {temp = 0; prepare = 0; memset (need, 0, 4); While (temp <4) {If (TMP >=( data_len-keys _count) {break ;} prepare = (prepare <6) | (find_pos (data [TMP]); temp ++; TMP ++;} prepare = prepare <(4-temp) * 6); for (I = 0; I <3; I ++) {if (I = temp) {break;} * f = (char) (prepare> (2-I) * 8) & 0xff); F ++;} * f = '\ 0'; return ret ;}

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.