These days, use Base64 encoding, find a good, collection
Base64 is one of the commonly used coding methods in MIME messages. Its main idea is to encode the input string or data into only {'A' - 'Z', 'A' - 'Z', '0' - '9', ' ', '/'} 64 strings that printable characters are called "Base64".
The Base64 encoding method is to take the input data stream each time 6 bit, use this 6 bit value (0-63) as an index to check the table, output the corresponding character. In this way, each 3 bytes are encoded to 4 characters (3 × 8 → 4 × 6); filled with '=' by dissatisfaction with 4 characters.
Const char enbase64tab [] = "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 /";
Int Encodebase64 (Const unsigned char * psrc, char * pdst, int nsrclen, int nmaxlinelen)
{
Unsigned CHAR C1, C2, C3; // Input buffer reading 3 bytes
INT ndstlen = 0; // output character count
INT NLINELEN = 0; // Output of long-term count
INT NDIV = nsrclen / 3; // Enter data length divided by 3
INT nMOD = nsrclen% 3; // Input data length divided by 3
/ / Take 3 bytes each time, encode 4 characters
For (int i = 0; i { // Take 3 bytes C1 = * psrc ; C2 = * psrc ; C3 = * psrc ; / / Encode into 4 characters * PDST = Enbase64TAB [C1 >> 2]; * PDST = eNBase64TAB [((C1 << 4) | (C2 >> 4)) & 0x3f]; * PDST = eNBase64TAB [((C2 << 2) | (C3 >> 6)) & 0x3f]; * PDST = Enbase64TAB [C3 & 0x3f]; NLINELEN = 4; NDSTLEN = 4; / / Output wrap? IF (nLinelen> NmaxLinelen - 4) { * PDST = '/ r'; * PDST = '/ n'; NLINELEN = 0; NDSTLEN = 2; } } // Code the remaining bytes IF (nmod == 1) { C1 = * psrc ; * PDST = Enbase64TAB [(C1 & 0xFC) >> 2]; * PDST = Enbase64TAB [((C1 & 0x03) << 4)]; * PDST = '='; * PDST = '='; nLinelen = 4; NDSTLEN = 4; } Else IF (nmod == 2) { C1 = * psrc ; C2 = * psrc ; * PDST = Enbase64TAB [(C1 & 0xFC) >> 2]; * PDST = eNBase64TAB [((C1 & 0x03) << 4) | ((C2 & 0xF0) >> 4)]; * PDST = Enbase64TAB [((C2 & 0x0f) << 2)]; * PDST = '='; NDSTLEN = 4; } // Output add a end value * PDST = '/ 0'; Return ndstlen; } In the Base64 decoding method, the simplest is also a checklist: the value of 64 printable characters as an index, the value obtained by the table (range is 0-63) is connected in turn, the patchwork is output, and the decoding is obtained. result. Const char debase64tab [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, // ' ' 0, 0, 0, 63, // '/' 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0' - '9' 0, 0, 0, 0, 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, // 'A' - 'Z' 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, // 'A' - 'Z' } INT decodebase64 (const char * psrc, unsigned char * pdst, int nsrclen) { INT ndstlen; // output character count INT nvalue; // Decoding used long intensity INT I; i = 0; Ndstlen = 0; // Take 4 characters, decode to a long integer, then shift to get 3 bytes While (i { IF (* psrc! = '/ r' && * psrc! = '/ n') { NValue = debase64tab [* psrc ] << 18; NValue = debase64tab [* psrc ] << 12; * PDST = (nvalue & 0x00ff0000) >> 16; NDSTLEN ; IF (* psrc! = '=') { NValue = debase64tab [* psrc ] << 6; * PDST = (nvalue & 0x0000FF00) >> 8; NDSTLEN ; IF (* psrc! = '=') { NValue = debase64tab [* psrc ]; * PDST = nvalue & 0x000000FF; NDSTLEN ; } } i = 4; } ELSE // Enter back, skip { PSRC ; i ; } } // Output add a end value * PDST = '/ 0'; Return ndstlen; }