Base64 encoder

This is a discussion on Base64 encoder within the C Programming forums, part of the General Programming Boards category; Hello. Someone know if exist any API or any way to encode a ANSI string to Base64? Thanks...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    39

    Base64 encoder

    Hello.


    Someone know if exist any API or any way to encode a ANSI string to Base64?


    Thanks

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Should be free libraries floating around out there. GIYF.

  3. #3
    EOP
    EOP is offline
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    Code:
    CString Base64Encode(LPCTSTR src)
    {
    	if( !src )
    		return( _T("") );
    
    	int size = static_cast<int> (strlen( src ));
    	CString out;
    	LPSTR p = out.GetBuffer( size*4/3+5);
    
    	for(int i=0; i<size; i+=3)
    	{
    		unsigned char b1=0, b2=0, b3=0, b4=0, b5=0, b6=0, b7=0;
    
    		b1 = src[i];
    
    		if(i+1<size)
    			b2 = src[i+1];
    
    		if(i+2<size)
    			b3 = src[i+2];
    
    		b4= b1>>2;
    		b5= ((b1&0x3)<<4)|(b2>>4);
    		b6= ((b2&0xf)<<2)|(b3>>6);
    		b7= b3&0x3f;
    
    		*p++= encode(b4);
    		*p++= encode(b5);
    
    		if(i+1<size) {
    			*p++= encode(b6);
    		} else {
    			*p++= '=';
    		}
    
    		if(i+2<size) {
    			*p++= encode(b7);
    		} else {
    			*p++= '=';
    		}
    
    	}
    	*p = '\0';
    	out.ReleaseBuffer();
    	return( out );
    }
    
    
    // Base64 encode one byte
    char CBasicAuth::encode(unsigned char u)
    {
    	if(u < 26)  return 'A' + u;
    	if(u < 52)  return 'a' + (u - 26);
    	if(u < 62)  return '0' + (u - 52);
    	if(u == 62) return '+';
    
    	return '/';
    }

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    This isn't a search engine, don't encourage it.

    Not to mention it's C++ in the C section.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Not to also mention it appears to be MFC/Windows-specific C++.

  6. #6
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. win32 mp3 encoder?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-10-2007, 04:05 AM
  2. Zebra P310 Encoder
    By newgirl in forum Networking/Device Communication
    Replies: 3
    Last Post: 10-25-2004, 12:38 AM
  3. ASCII Art Decoder and Encoder
    By jessweetd in forum C Programming
    Replies: 7
    Last Post: 09-05-2004, 07:12 PM
  4. Need RSA encoder / decoder / keygen
    By Rak'kar in forum Networking/Device Communication
    Replies: 4
    Last Post: 07-20-2004, 09:40 PM
  5. Encoder Issue
    By CodeMonkey in forum C++ Programming
    Replies: 4
    Last Post: 10-19-2002, 11:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21