zoukankan      html  css  js  c++  java
  • vb base64 操作

    '将二进制文件转换成base64编码
    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

    '从base64编码过的文件还原成二进制文件
    Public Function ConvertFileFromBase64(ByVal fromName As String, ByVal toName As String)
        Dim TextLine
        Open fromName For Input As #1 ' 打开文件。
        Do While Not EOF(1) ' 循环至文件尾。
            Line Input #1, TextLine ' 读入一行数据并将其赋予某变量。
        Loop
        Close #1 ' 关闭文件。
        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

    '读一个文本文件
    Public Function ReadTextFile(ByVal fileName As String) As String
        Dim TextLine
        Open fileName For Input As #1 ' 打开文件。
        Do While Not EOF(1) ' 循环至文件尾。
            Line Input #1, TextLine ' 读入一行数据并将其赋予某变量。
        Loop
        Close #1 ' 关闭文件。
        ReadTextFile = TextLine
    End Function

     '写一个文本文件
    Public Function WriteTextFile(ByVal fileName As String, ByVal strToWrite As String)
        Open fileName For Output As #fnum
        Print #fnum, strToWrite
        Close #fnum
    End Function


     

  • 相关阅读:
    [转]面向对象的三个基本特征
    C#验证Email
    天气预报
    【原】c#实现数字金额转换成大写金额
    C#发送Email
    DIV 显示最上层
    ArrayList 与 String[] 之间的转换
    Flex与.NET互操作(五):FileReference+HttpHandler实现上传/下载
    Flex与.NET互操作(三):基于WebService的数据访问(下)
    Flex与.NET互操作(六):Flex和.NET协同开发利器FluorineFx
  • 原文地址:https://www.cnblogs.com/yg_zhang/p/921522.html
Copyright © 2011-2022 走看看