Use ADO stream for base64 encoding and decoding

Source: Internet
Author: User

'*************************************** * <Br/> '* Module name: modbase64.bas <br/> '* module functions: base64 encoding and decoding functions <br/>' * Author: lyserver <br/> '********************************** * ***** </P> <p> Option explicit </P> <p> '-------------------------------------------------<br/>' function description: base64 encoding <br/> '---------------------------------------------<br/> Public Function base64encode (Varin as variant) as string <br/> dim adostream as object <br/> dim xmldoc as object <br/> dim xmlnode as object </P> <p> set adostream = Createobject ("ADODB. stream ") <br/> adostream. charset = "gb2312" <br/> If vartype (Varin) = vbstring then <br/> adostream. type = 2 'adtypetext <br/> adostream. open <br/> adostream. writetext Varin <br/> elseif vartype (Varin) = vbbyte or vbarray then <br/> adostream. type = 1 'adtypebinary <br/> adostream. open <br/> adostream. write Varin <br/> else <br/> exit function <br/> end if <br/> adostream. position = 0 <br/> adostream. type = 1 'adtypebinary </P> <p> set xmldoc = Createobject ("msxml2.domdocument") <br/> set xmlnode = xmldoc. createelement ("mynode") <br/> xmlnode. datatype = "bin. base64 "<br/> xmlnode. nodetypedvalue = adostream. read <br/> base64encode = xmlnode. text <br/> adostream. close <br/> end function </P> <p> '---------------------------------------------<br/>' function description: base64 decoding <br/> '-------------------------------------------<br/> Public Function base64decode (Varin as variant, optional byval returnvaluetype as vbvartype = vbstring) as byte () <br/> dim adostream as object <br/> dim xmldoc as object <br/> dim xmlnode as object </P> <p> set xmldoc = Createobject ("msxml2.domdocument ") <br/> set xmlnode = xmldoc. createelement ("mynode") <br/> xmlnode. datatype = "bin. base64 "<br/> If vartype (Varin) = vbstring then <br/> xmlnode. TEXT = Replace (Varin, vbcrlf, "") <br/> elseif vartype (Varin) = vbbyte or vbarray then <br/> xmlnode. TEXT = Replace (strconv (Varin, vbunicode), vbcrlf ,"") <br/> else <br/> exit function <br/> end if </P> <p> set adostream = Createobject ("ADODB. stream ") <br/> adostream. charset = "gb2312" <br/> adostream. type = 1 'adtypebinary <br/> adostream. open <br/> adostream. write xmlnode. nodetypedvalue <br/> adostream. position = 0 <br/> If returnvaluetype = vbstring then <br/> adostream. type = 2 'adtypetext <br/> base64decode = adostream. readtext <br/> else <br/> base64decode = adostream. read <br/> end if <br/> adostream. close <br/> end Function

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.