Base64 codec code (cximage class processing)

Source: Internet
Author: User

# Include <string> <br/> using namespace STD; <br/> class zbase64 <br/>{< br/> public: <br/> // encoding <br/> // databyte <br/> // [in] the length of the input data, in bytes <br/> // <br/> string encode (const unsigned char * data, int databyte ); <br/> // decoding <br/> // databyte <br/> // [in] the length of the input data, in bytes <br/> // outbyte <br/> // [out] the length of the output data, in bytes, do not calculate the length of the output data using the return value <br/> string decode (const char * data, int databyte, Int & outbyte); <Br/>}; <br/> # include "stdafx. H "<br/> # include" zbase64.h "<br/> string zbase64: encode (const unsigned char * data, int databyte) <br/> {<br/> // encoding table <br/> const char encodetable [] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 + /"; <br/> // return value <br/> string strencode; <br/> unsigned char TMP [4] = {0}; <br/> int linelength = 0; <br/> for (INT I = 0; I <(INT) (databyte/3); I ++) <br/>{< br/> TM P [1] = * Data ++; <br/> TMP [2] = * Data ++; <br/> TMP [3] = * Data ++; <br/> strencode + = encodetable [TMP [1]> 2]; <br/> strencode + = encodetable [(TMP [1] <4) | (TMP [2]> 4) & 0x3f]; <br/> strencode + = encodetable [(TMP [2] <2) | (TMP [3]> 6) & 0x3f]; <br/> strencode + = encodetable [TMP [3] & 0x3f]; <br/> If (linelength + = 4, linelength = 76) {strencode + = "/R/N"; linelength = 0 ;} <br/>}< br/> // encode the remaining data <br/> Int mod = databyte % 3; <br/> If (mod = 1) <br/>{< br/> TMP [1] = * Data ++; <br/> strencode + = encodetable [(TMP [1] & 0xfc)> 2]; <br/> strencode + = encodetable [(TMP [1] & 0x03) <4)]; <br/> strencode + = "= "; <br/>}< br/> else if (mod = 2) <br/>{< br/> TMP [1] = * Data ++; <br/> TMP [2] = * Data ++; <br/> strencode + = encodetable [(TMP [1] & 0xfc)> 2]; <br/> strencode + = encodetable [(TMP [1] & 0x03) <4) | (( TMP [2] & 0xf0)> 4)]; <br/> strencode + = encodetable [(TMP [2] & 0x0f) <2)]; <br/> strencode + = "="; <br/>}</P> <p> return strencode; <br/>}< br/> string zbase64 :: decode (const char * data, int databyte, Int & outbyte) <br/> {<br/> // decoding table <br/> const char decodetable [] = <br/> {<br/> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, <br/> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, <br/> 62, // '+' <br/> 0, 0, 0, <br/> 63, // '/' <br/> 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9' <br/> 0, 0, 0, 0, 0, 0, <br/> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, <br/> 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'a'-'Z' <br/> 0, 0, 0, 0, 0, 0, <br/> 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, <br/> 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'Z' <br/>}; <br/> // return value <br/> string strdecode; <br/> int nvalue; <br/> int I = 0; <br/> while (I <databyte) <br/>{< br/> If (* Data! = '/R' & * Data! = '/N') <br/>{< br/> nvalue = decodetable [* Data ++] <18; <br/> nvalue + = decodetable [* Data ++] <12; <br/> strdecode + = (nvalue & 0x00ff0000)> 16; <br/> outbyte ++; <br/> If (* Data! = ') <Br/>{< br/> nvalue + = decodetable [* Data ++] <6; <br/> strdecode + = (nvalue & 0x0000ff00)> 8; <br/> outbyte ++; <br/> If (* Data! = ') <Br/>{< br/> nvalue + = decodetable [* Data ++]; <br/> strdecode + = nvalue & 0x000000ff; <br/> outbyte ++; <br/>}< br/> I + = 4; <br/>}< br/> else // press enter to wrap the line. <br/>{< br/> data ++; <br/> I ++; <br/>}< br/> return strdecode; <br/>}< br/> example (combined with the cximage Library ): <br/> cstring cscandlg: encodeimage () <br/> {// base64-encoded image <br/> zbase64 zbase; <br/> // image encoding <br/> cximage image; // define a cximage object <br/> image. load (this-> m_strimgpath, cximage_format_jpg); // first load the JPG file and specify the file type. <br/> long size = 0; // obtain the image size <br/> byte * buffer = 0; // buffer for storing image data <br/> image. encode (buffer, size, cximage_format_jpg); // copy the image in the image object to buffer as type data <br/> string strtmpresult = zbase. encode (buffer, size); <br/> cstring result; <br/> result = strtmpresult. c_str (); <br/> return result; <br/>}< br/> void cscandlg: decodeimagedata (cstring strdata) <br/> {// decodes base64 encoded data and displays the original image. <br/> zbase64 zbase; <br/> int outbyte = 0; <br/> string strtmpresult = zbase. decode (strdata, strdata. getlength (), outbyte); <br/> int I, Len = strtmpresult. length (); <br/> byte * buffer = new byte [Len]; <br/> for (I = 0; I <Len; ++ I) <br/>{< br/> buffer [I] = strtmpresult [I]; <br/>}< br/> cximage image (buffer, Len, cximage_format_jpg ); // construct the data in the memory buffer into an image object <br/> Delete [] buffer; <br/> CDC * HDC = m_picture.getdc (); <br/> m_bitmap = image. makebitmap (HDC-> m_hdc); <br/> hbitmap h0ldbmp = m_picture.setbitmap (m_bitmap); <br/> If (h0ldbmp) deleteobject (h0ldbmp ); <br/> If (HDC-> m_hdc) m_picture.releasedc (HDC); <br/> If (m_bitmap) deleteobject (m_bitmap); <br/>} 

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.