1

我有一个输出 .bmp 验证码图像的脚本。

图像以十六进制构建,并转换为二进制并通过response.binaryWrite chrB(CByte(myHexImage))(作为图像 mime 类型 = bmp)发送到浏览器

我希望选择摆脱这种情况(更改 mime 类型等),而只向输出发送一些内容,如下所示:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2 ...

(除了我的图像是BMP)

有没有一种快速简便的方法可以在 vbscript 中将该十六进制或二进制转换为 base64?如上所述,这是我现在实施的一个片段。

我怎样才能改变这个,所以我输出到屏幕上,有效的十六进制格式(然后我可以很容易地转换为base64)或直接base64?

    Dim sBmpEndLine, sBmpInfoHeader, sBmpHeader, sTmpHex

    If (m_iBmpWidth Mod 4) <> 0 Then
        sBmpEndLine = string((4 - (m_iBmpWidth Mod 4)) * 2, "0")
    Else
        sBmpEndLine = vbNullString
    End If

    sBmpInfoHeader = array("28000000", "00000000", "00000000", "0100", "0800", "00000000", "00000000", "120B0000", "120B0000", "00000000", "00000000")
    sBmpInfoHeader(1) = formatHex(hex(m_iBmpWidth), 4, 0, True)
    sBmpInfoHeader(2) = formatHex(hex(m_iBmpHeight), 4, 0, True)
    sBmpInfoHeader(6) = formatHex(hex((m_iBmpHeight * m_iBmpWidth) + (m_iBmpHeight * (len(sBmpEndLine) / 2))), 4, 0, True)
    sBmpInfoHeader(9) = formatHex(hex(len(m_sBmpColorMap) / 8), 4, 0, True)
    sBmpInfoHeader(10) = sBmpInfoHeader(9)
    sBmpHeader = array("424D", "00000000", "0000", "0000", "00000000")
    sBmpHeader(1) = formatHex(hex((len(join(sBmpHeader, "")) / 2) + (len(join(sBmpInfoHeader, "")) / 2) + (len(m_sBmpColorMap) / 2) + (m_iBmpHeight * m_iBmpWidth) + (m_iBmpHeight * (len(sBmpEndLine) / 2))), 4, 0, True)
    sBmpHeader(4) = formatHex(hex((len(join(sBmpHeader, "")) / 2) + (len(join(sBmpInfoHeader, "")) / 2) + (len(m_sBmpColorMap) / 2)), 4, 0, True)

    sendHex(join(sBmpHeader, ""))
    sendHex(join(sBmpInfoHeader, ""))
    sendHex(m_sBmpColorMap)
    For y = m_iBmpHeight To 1 Step -1
        For x = 1 To m_iBmpWidth
            sTmpHex = m_aBitmap(y, x)
            If sTmpHex = vbNullString Then
                sendHex(m_sBgColor)
            Else
                sendHex(sTmpHex)
            End If
        Next
        sendHex(sBmpEndLine)
    Next

    Response.Flush

这是sendHex()功能:

Private Sub sendHex(valHex)

    Dim iCntHex
    For iCntHex = 1 To len(valHex) Step 2
        'Response.BinaryWrite chrB(CByte("&H" & mid(valHex, iCntHex, 2)))
        response.Write "&H" & mid(valHex, iCntHex, 2)
    Next
End Sub
4

2 回答 2

1

Microsoft.XMLDOM内置了 和 的bin.base64转换器bin.hex。我编写了演示如何使用它的函数:

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

以下是它们的用法示例:

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

还可以将其ADODB.Stream视为处理二进制文件的一种方式。它将适用于这些例程。

于 2013-03-17T05:49:03.503 回答
0

我能够得到这个工作。这里是如何。

在 sendHex 中,我删除了该&H部分,并将字符串包裹在 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

这会产生这样的字符串输出(以 2 个十六进制字符的字节字符串形式):

424d1e050000000000003e00000028000000340000001800000001000

然后我可以将正确的十六进制字符串转储到 aHEXbase64,如下所示(不是我写的,而是 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

这会将上面的内容转换为 base64,我可以使用这样的输出(例如在浏览器 url 栏中)将其作为图像查看:

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

于 2013-01-24T18:24:14.230 回答