Visual Basic 6 function "convert_uudecode"

Go back

Below you'll find the source for the Visual Basic 6 function convert_uudecode.

Attribute VB_Name = "modConvertUudecode"
' These functions are downloaded from:
' http://www.stefanthoolen.nl/archive/vb6-functions/
' 
' You may freely distribute this file but please leave all comments, including this one, in it.
' 
' @Author Stefan Thoolen <mail@stefanthoolen.nl>

Option Explicit

''
' Makes a binary string from an integer number
' Same syntax as the PHP function 'decbin'
' See also: http://www.php.net/manual/en/function.decbin.php
' @param    Integer number          The decimal value
' @return   String                  A binary presentation of the number (ex.: 00100111)
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Function decbin(ByVal number As Integer) As String
    Dim retval As String
    Do Until number = 0
        If (number Mod 2) Then retval = "1" & retval Else retval = "0" & retval
        number = number \ 2
    Loop
    decbin = retval
End Function

''
' Makes an integer number from a binary string
' Same syntax as the PHP function 'bindec'
' See also: http://www.php.net/manual/en/function.bindec.php
' @param    String  binary_string   The binary string (ex.: 00100111)
' @return   Integer                 A decimal presentation of the binary value
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Function bindec(binary_string As String) As Long
    Dim i As Integer, pos As Integer, ret As Long
    For i = 1 To Len(binary_string)
        pos = Len(binary_string) - i
        If Mid(binary_string, pos + 1, 1) = "1" Then ret = ret + (2 ^ (i - 1))
    Next i
    bindec = ret
End Function

''
' Decode a uuencoded string
' Same syntax as the PHP function 'convert_uudecode'
' See also: http://www.php.net/manual/en/function.convert-uudecode.php
' @param    String  str         The uuencoded version of a string
' @return   String              A text/plain presentation of the string
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Function convert_uudecode(str As String) As String
    Dim i As Integer, j As Integer, k As Integer, s As String
    Dim binstr As String, retval As String
    
    ReDim p(1 To 1) As String
    p = Split(str, Chr(10))
    For i = LBound(p) To UBound(p)
        binstr = ""
        ' Converts the string to binary data
        For j = 1 To Len(p(i))
            s = decbin(Asc(Mid(p(i), j, 1)))
            s = String(8 - Len(s), "0") & s
            s = Right(decbin(bindec(s) + 32), 6)
            binstr = binstr & s
        Next j
        ' The first 6-sized byte contains the length of the line
        k = bindec(Left(binstr, 6)): binstr = Right(binstr, Len(binstr) - 6)
        For j = 1 To k * 8 Step 8
            retval = retval & Chr(bindec(Mid(binstr, j, 8)))
        Next j
    Next i
    
    convert_uudecode = retval
End Function