Convert hex string (image) to base64 (for browser rendering) in VBScript

后端 未结 2 844
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 11:52

I have a script that outputs a .bmp captcha image.

The image is built in hexadecimal, and converted to binary and sent to the browser via response.binaryWrite

相关标签:
2条回答
  • 2021-01-07 12:09

    The Microsoft.XMLDOM has built in converters for bin.base64 and bin.hex. I wrote functions that demonstrate how to use this:

    Function TextToBinary(text, dataType)
      Dim dom
      Set dom = CreateObject("Microsoft.XMLDOM")
      dom.loadXML("<HELLO/>")
      dom.documentElement.nodeTypedValue = text
      dom.documentElement.dataType = dataType
      TextToBinary = dom.documentElement.nodeTypedValue
    End Function
    
    Function BinaryToText(binary, dataType)
      Dim dom
      Set dom = CreateObject("Microsoft.XMLDOM")
      dom.loadXML("<HELLO/>")
      dom.documentElement.dataType = dataType
      dom.documentElement.nodeTypedValue = binary
      dom.documentElement.removeAttribute("dt:dt")
      BinaryToText = dom.documentElement.nodeTypedValue
    End Function
    
    Function HexToBase64(strHex)
      HexToBase64 = BinaryToText(TextToBinary(strHex, "bin.hex"), "bin.base64")
    End Function
    
    Function Base64ToHex(strBase64)
      Base64ToHex = BinaryToText(TextToBinary(strBase64, "bin.base64"), "bin.hex")
    End Function
    

    Here's an example of their usage:

    MsgBox HexToBase64("41")
    MsgBox Base64ToHex("QQ==")
    

    Also look at the ADODB.Stream as a means of working with binary files. It'll work with these routines.

    0 讨论(0)
  • 2021-01-07 12:15

    I was able to get this working. Here is how.

    In sendHex, I removed the &H portion, and wrapped my string in hex():

    Private Sub sendHex(valHex)
        Dim iCntHex
        For iCntHex = 1 To len(valHex) Step 2
        If len( mid(valHex, iCntHex, 2)) = 1 Then 
            response.write "0"
        end if 
        response.write mid(valHex, iCntHex, 2)
        Next
    End Sub
    

    This results in a string output like this (in byte strings of 2 hexidecimal chars):

    424d1e050000000000003e00000028000000340000001800000001000

    I can then dump that proper hex string into a HEX to base64 function as follows (not written by me, but rather, by Richard Mueller - http://www.rlmueller.net/Base64.htm)

    Function HexToBase64(strHex)
        ' Function to convert a hex string into a base64 encoded string.
        ' Constant B64 has global scope.
        Dim lngValue, lngTemp, lngChar, intLen, k, j, strWord, str64, intTerm
    
        intLen = Len(strHex)
    
        ' Pad with zeros to multiple of 3 bytes.
        intTerm = intLen Mod 6
        If (intTerm = 4) Then
            strHex = strHex & "00"
            intLen = intLen + 2
        End If
        If (intTerm = 2) Then
            strHex = strHex & "0000"
            intLen = intLen + 4
        End If
    
        ' Parse into groups of 3 hex bytes.
        j = 0
        strWord = ""
        HexToBase64 = ""
        For k = 1 To intLen Step 2
            j = j + 1
            strWord = strWord & Mid(strHex, k, 2)
            If (j = 3) Then
                ' Convert 3 8-bit bytes into 4 6-bit characters.
                lngValue = CCur("&H" & strWord)
    
                lngTemp = Fix(lngValue / 64)
                lngChar = lngValue - (64 * lngTemp)
                str64 = Mid(B64, lngChar + 1, 1)
                lngValue = lngTemp
    
                lngTemp = Fix(lngValue / 64)
                lngChar = lngValue - (64 * lngTemp)
                str64 = Mid(B64, lngChar + 1, 1) & str64
                lngValue = lngTemp
    
                lngTemp = Fix(lngValue / 64)
                lngChar = lngValue - (64 * lngTemp)
                str64 = Mid(B64, lngChar + 1, 1) & str64
    
                str64 = Mid(B64, lngTemp + 1, 1) & str64
    
                HexToBase64 = HexToBase64 & str64
                j = 0
                strWord = ""
            End If
        Next
        ' Account for padding.
        If (intTerm = 4) Then
            HexToBase64 = Left(HexToBase64, Len(HexToBase64) - 1) & "="
        End If
        If (intTerm = 2) Then
            HexToBase64 = Left(HexToBase64, Len(HexToBase64) - 2) & "=="
        End If
    
    End Function
    

    This converts the above to base64, and I can use the output like this (e.g. in a browser url bar) to view it as an image:

    data:image/bmp;base64,Qk0eBQAAAAAAAD4AAAAo...

    0 讨论(0)
提交回复
热议问题