Home » Source Code » » Base64编码.c

Base64编码.c ( File view )

From:
  • By 2010-08-23
  • View(s):3
  • Download(s):0
  • Point(s): 1
			#include <stdio.h>
#include <conio.h>
#include <mem.h>
#include <malloc.h>

char* EncodeBase64(const char* pVal ,int len)
{

    static const char*index= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    const char* p = pVal;
    //char*const pRet = new char[len*4/3+16];
    char*const pRet = (char*)malloc(len*4/3+16);
    memset( pRet , 0 , len*4/3+16 );
    char* pOut=pRet;
    while( len >=3 ){

        pOut[0] = index[(p[0]>>2)&0x3f];
        pOut[1] = index[(((p[0]<<6)>>2)| (p[1]>>4))&0x3f];
        pOut[2] = index[((p[1]<<4>>2) | (p[2]>>6))&0x3f];
        pOut[3] = index[(p[2]<<2>>2)&0x3f];
        len-=3;
        p+=3;
        pOut+=4;
        pOut[0]=0;
    
}
    if( 2==len){

        pOut[0] = index[(p[0]>>2)&0x3f];
        pOut[1] = index[(((p[0]<<6)>>2)| (p[1]>>4))&0x3f];
        pOut[2] = index[(p[1]<<4>>2)&0x3f];
        pOut[3] = '=';
        pOut[4]=0;
    
}else if( 1==len){

        pOut[0] = index[(p[0]>>2)&0x3f];
        pOut[1] = index[((p[0]<<6)>>2)&0x3f];
        pOut[2] = '=';
        pOut[3] = '=';
        pOut[4]=0;
    
}
    return pRet;

}
int DecodeBase64(const char* p,char* pOut)
{

    int len =0;
    size_t i;
    static const char index[]={
62,0,0,0,63,52,53,54,55,56,57,58,59,60,61,
        0,0,0,64,0,0,0,0,1,2,3,4,5,6,7,8,9,
        10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
        0,0,0,0,0,0,26,27,28,29,30,31,32,33,
        34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51
};
    size_t l = strlen( p );
    for(i=0; i<l-4;i+=4){

        pOut[0] = index[ p[0]-0x2b ]<<2|index[ p[1]-0x2b ]<<2>>6;
        pOut[1] = index[ p[1]-0x2b ]<<4|index[ p[2]-0x2b ]<<2>>4;
        pOut[2] = index[ p[2]-0x2b ]<<6|index[ p[3]-0x2b ]<<2>>2;
        pOut+=3;
        len+=3;
        p+=4;
...
...
(Please download the complete source code to view)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
Base64编码.c2.65 kB22-08-08|10:09
...
Sponsored links

Base64编码.c (1.14 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D