VB base64 operation

Source: Internet
Author: User

'Convert the binary file to base64 encoding
Public Function convertfiletobase64 (byval filename) as string
Dim file_length as long
Dim fnum as integer
Dim bytes () as byte
Dim TXT as string
Dim I as integer
File_length = filelen (filename)
Fnum = freefile
Redim bytes (1 to file_length)
Open filename for binary as # fnum
Get # fnum, 1, bytes
Close fnum
TMP = encodebase64 (bytes)
Convertfiletobase64 = TMP
End Function

private function encodebase64 (byref arrdata () as byte) as string
dim objxml as msxml2.domdocument
dim objnode as msxml2.ixmldomelement
'help from MSXML
set objxml = new msxml2.domdocument
'byte array to base64
set objnode = objxml. createelement ("b64")
objnode. datatype = "bin. base64 "
objnode. nodetypedvalue = arrdata
encodebase64 = objnode. text
'thanks, bye
set objnode = nothing
set objxml = nothing
end function

private function decodebase64 (byval strdata as string) as byte ()
dim objxml as msxml2.domdocument
dim objnode as msxml2.ixmldomelement
'help from MSXML
set objxml = new msxml2.domdocument
set objnode = objxml. createelement ("b64")
objnode. datatype = "bin. base64 "
objnode. TEXT = strdata
decodebase64 = objnode. nodetypedvalue
'thanks, bye
set objnode = nothing
set objxml = nothing
end function

'Restore a base64 encoded file to a binary file
Public Function convertfilefrombase64 (byval fromname as string, byval toname as string)
dim textline
open fromname for input as #1 'open the file.
do while not EOF (1) 'loops to the end of the file.
line Input #1, textline' reads a row of data and assigns it to a variable.
loop
close #1 'close the file.
dim A () as byte
'redim A (1 to Len (textline)
A = decodebase64 (textline)
fnum = freefile
open toname for binary as # fnum
put # fnum, 1, A
close fnum
end function

'Read a text file
Public Function readtextfile (byval filename as string) as string
Dim textline
Open filename for input as #1 'open the file.
Do while not EOF (1) 'loops to the end of the file.
Line input #1, textline' reads a row of data and assigns it to a variable.
Loop
Close #1 'close the file.
Readtextfile = textline
End Function

'Write a text file
Public Function writetextfile (byval filename as string, byval strtowrite as string)
Open filename for output as # fnum
Print # fnum, strtowrite
Close # fnum
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.