Posts Tagged ‘function’

VB6/VBA functions to convert binary string to Base64 string

Tuesday, August 16th, 2011

Here are some functions to convert a binary string, to a byte array, to a Base64 string and then back to a byte array and binary string. Run tester to see it in action, enjoy.

Private Function encodeBase64(ByRef arrData() As Byte) As String
    Dim objXML As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMElement

    Set objXML = New MSXML2.DOMDocument

    Set objNode = objXML.createElement("b64")
    objNode.DataType = "bin.base64"
    objNode.nodeTypedValue = arrData
    encodeBase64 = objNode.Text

    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

    Set objXML = New MSXML2.DOMDocument
    Set objNode = objXML.createElement("b64")
    objNode.DataType = "bin.base64"
    objNode.Text = strData
    decodeBase64 = objNode.nodeTypedValue

    Set objNode = Nothing
    Set objXML = Nothing
End Function

Function bin2Byte(ByVal s As String) As Byte()
    Dim bitsIn As Long
    bitsIn = 8

    Dim i As Long
    'pad with zeros
    If Len(s) Mod bitsIn <> 0 Then
        For i = 1 To bitsIn - Len(s) Mod bitsIn
            s = "0" & s
        Next i
    End If

    i = Len(s)
    Dim bytes() As Byte
    Dim byteCount As Long
    byteCount = -1
    Dim sByte As String
    Do While LenB(s) > 0
        byteCount = byteCount + 1
        ReDim Preserve bytes(byteCount)

        sByte = Mid$(s, Len(s) - bitsIn + 1)
        'sByte = Mid$(s, 1, bitsIn)
        For i = 0 To 7 Step 1
            bytes(byteCount) = bytes(byteCount) + CLng(Mid$(sByte, 8 - i, 1)) * 2 ^ i
        Next i
        s = Mid$(s, 1, Len(s) - bitsIn)
        's = Mid$(s, bitsIn + 1)
    Loop
    bin2Byte = bytes
End Function

Function byte2Bin(ByRef bytes() As Byte) As String
    Dim i As Long, j As Long
    Dim bin As String
    For i = 0 To UBound(bytes)
        bin = Space$(8)

        For j = 0 To 7
            If bytes(i) And 2 ^ j Then
                Mid(bin, 8 - j, 1) = "1"
            Else
                'Mid(bin, 8 - j, 1) = "0"
            End If
        Next j

        byte2Bin = bin & byte2Bin
    Next i
    byte2Bin = LTrim$(byte2Bin)
    byte2Bin = Replace(byte2Bin, " ", "0", 1, -1, vbBinaryCompare)
End Function

Sub tester()
    'note we can't add any 0 padding to the test binary string
    Dim bin As String
    bin = "111101000001100010101"
    Dim binOut As String
    binOut = byte2Bin(decodeBase64(encodeBase64(bin2Byte(bin))))

    MsgBox binOut = bin
End Sub

Thanks to Tim Hastings for the Base64 functions.

Related posts:

  1. Base64/sexatrigesimal encoding/decoding in VBA/VB6/Visual Basic
  2. Decimal to Binary functions in Visual Basic
  3. Optimizing/faster String Concatenation in VBA

Simple Ping function VBScript

Monday, November 15th, 2010

I was after a ping function that met the following constraints;

  • Didn’t use GetObject.
  • Didn’t display any command (cmd) prompt.
  • Didn’t use a temporary files.

Here is what I found;

<br />
Function ping(node)<br />
    Set WshShell = CreateObject(&quot;WScript.Shell&quot;)<br />
    ping = Not CBool(WshShell.Run(&quot;ping -n 1 &quot; &amp; node, 0, True))<br />
End Function<br />

I was looking at a way to timeout the ping function after 1 second using Threading but this didn’t quite make it so simple. Whilst ping offers a timeout value if you ping an incorrect hostname it will hang for a little while.

Related posts:

  1. Generic file selection window function in VBA
  2. Visual Basic 6 – quickest way to find first/last character in string
  3. Rigid file manipulation functions for VBA/VBS

In Excel, find the value on the last row as a function, not VBA

Monday, July 20th, 2009

Here is a quick post, how to find the value of the last cell in a column.

Exaample below uses Column A with the first 3 rows used for the header:

=INDEX(A:A,COUNT(A:A)+3)

Related posts:

  1. VBA automatically saves Excel 2003 Workbook in compatibility mode as Excel 2007 Workbook
  2. Generic file selection window function in VBA
  3. Add URL link to WordPress default header