Link to home
Start Free TrialLog in
Avatar of Jsan
Jsan

asked on

UUencode UUDecode

When you want to email a binary file without sending it as an attatchment UUencode translates non-printable characters into printable characters so that email programs can handle them to be sent.  It does this by taking 3 chars 8 bit each total 24 bits and stretching them out to 4 chars 6 bits each total 24 bits. 6 bits for each character means a total of 64 possible characters all of which can be used from the printable character set.  Does this sound familiar to anyone?  Does anyone have an algorithm for this?

Also any procedures for getting/setting specific bits in a byte, integer, word or bit array would be helpfull.
Avatar of Brendt Hess
Brendt Hess
Flag of United States of America image

Try this ActiveX control

http://www.coolstf.com/activex.html

and select the UUencode/uudecode control for download.  You'll have to register, but the control is free.

Also, check out:

http://www.vbip.com/winsock/winsock_uucode_01.asp

This is a tutorial on UUEncode/Decode with VB code examples
Avatar of plasmatek
plasmatek

Public Function UUDecode(ByVal B64String) As String

    Dim TableByte(255) As Integer
    Dim SexTet(4) As Integer
    Dim SexTetNUM As Integer
    Dim n As Integer
    Dim i As Integer
    Dim T1 As Integer
    Dim T2 As Integer
    Dim NumBits As Integer
    Dim OutStream As String
    Dim CharTable As String * 64
    Dim LiStream As Long
    Dim InStreamIndex As Long
    Dim c As Long
    CharTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

    For n = 0 To 255
        TableByte(n) = -1
    Next


    For n = 0 To 63
        i = Asc(Mid(CharTable, n + 1, 1))
        TableByte(i) = n
    Next

    InStreamIndex = 1
    LiStream = Len(B64String)
    OutStream = Space(Int(72 * Int(LiStream / 74) * 3 / 4) + Int((LiStream Mod 74) * 3 / 4))

    While InStreamIndex <= LiStream
        SexTetNUM = 1
        NumBits = 0

        While (SexTetNUM <= 4 And InStreamIndex <= LiStream)
            i = Asc(Mid(B64String, InStreamIndex, 1))
            InStreamIndex = InStreamIndex + 1
            T1 = TableByte(i)

            If T1 >= 0 Then
                SexTet(SexTetNUM) = T1
                NumBits = NumBits + 6
                SexTetNUM = SexTetNUM + 1
            End If

        Wend


        If NumBits >= 8 Then
            T1 = SexTet(1)
            T2 = SexTet(2)
            c = c + 1
            Mid(OutStream, c, 1) = Chr((4 * T1) + (T2 \ 16))
            NumBits = NumBits - 8
        End If


        If NumBits >= 8 Then
            T1 = SexTet(3)
            c = c + 1
            Mid(OutStream, c, 1) = Chr(16 * (T2 Mod 16) + (T1 \ 4))
            NumBits = NumBits - 8
        End If


        If NumBits >= 8 Then
            T2 = SexTet(4)
            c = c + 1
            Mid(OutStream, c, 1) = Chr(64 * (T1 Mod 4) + T2)
        End If

    Wend

    UUDecode = Left(OutStream, c)
End Function

Public Function UUEncode(ByVal text) As String

    Dim a1 As Integer
    Dim a2 As Integer
    Dim a3 As Integer
    Dim LineChars As Integer
    Dim OutStream As String
    Dim CharTable As String
    Dim i As Long
    Dim c As Long
    CharTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    c = 1
    i = Int(4 * Len(text) / 3)
    OutStream = Space(i + 2 * (i \ 72 + 1))
    LineChars = 0

    For i = 1 To Len(text) - 2 Step 3
        '      'Get 24 bits
        a1 = Asc(Mid(text, i, 1))
        a2 = Asc(Mid(text, i + 1, 1))
        a3 = Asc(Mid(text, i + 2, 1))

        '      'Encode each 6 bits
        Mid(OutStream, c, 4) = Mid(CharTable, (a1 And &HFC) \ 4 + 1, 1) _
                & Mid(CharTable, (16 * (a1 And &H3) + (a2 And &HF0) \ 16) + 1, 1) _
                & Mid(CharTable, (4 * (a2 And &HF) + (a3 And &HC0) \ 64) + 1, 1) _
                & Mid(CharTable, (a3 And &H3F) + 1, 1)
        c = c + 4

        '      'Break line
        LineChars = LineChars + 4

        If LineChars >= 72 Then
            LineChars = 0
            Mid(OutStream, c, 2) = vbCrLf
            c = c + 2
        End If

    Next

    '     'Add last 24 bits
    Select Case Len(text) Mod 3
        Case 1
            a1 = Asc(Mid(text, i, 1))
            a2 = 0
            a3 = 0
            Mid(OutStream, c, 4) = Mid(CharTable, (a1 And &HFC) \ 4 + 1, 1) _
                    & Mid(CharTable, (16 * (a1 And &H3) + (a2 And &HF0) \ 16) + 1, 1) _
                    & "==" & vbCrLf
            c = c + 6
        Case 2
            a1 = Asc(Mid(text, i, 1))
            a2 = Asc(Mid(text, i + 1, 1))
            a3 = 0
            Mid(OutStream, c, 4) = Mid(CharTable, (a1 And &HFC) \ 4 + 1, 1) _
                    & Mid(CharTable, (16 * (a1 And &H3) + (a2 And &HF0) \ 16) + 1, 1) _
                    & Mid(CharTable, (4 * (a2 And &HF) + (a3 And &HC0) \ 64) + 1, 1) _
                    & "=" & vbCrLf
            c = c + 6
    End Select

    UUEncode = Left(OutStream, c - 1)
End Function

there we go :)
Whoops - that's base64 Encoding, not uuEncoding in the above example.  uuEncoding starts at Space and goes to underscore (ascii 32 to ascii 95), with space normally replaced by ascii 96 (` - a grave accent).

uuEncode does not use lower case letters.
Avatar of Jsan

ASKER

Yes I would rather uuEncoding.  The reason being that I ran plasmatek's but the over head for converting a string was 73%  normal uencoding would have an overhead closer to 25%.

I looked at the example bhess1 gave me but the code doesn't run.  It blows up on the uuDecode function.

plasmatek's could you provide a normal uuEncoding example please?  Or someone point me to a sample UU Encode/Decode code set that works.


Jsan
What version of VB are you using?

Where did it blow up in the decode?
Avatar of Jsan

ASKER

Yes I would rather uuEncoding.  The reason being that I ran plasmatek's but the over head for converting a string was 73%  normal uencoding would have an overhead closer to 25%.

I looked at the example bhess1 gave me but the code doesn't run.  It blows up on the uuDecode function.

plasmatek's could you provide a normal uuEncoding example please?  Or someone point me to a sample UU Encode/Decode code set that works.


Jsan
Avatar of Jsan

ASKER

Im using VB 6.  Were you able to run the code successfully?  I tried to UU encode and then decode a small 9k gif file.
ASKER CERTIFIED SOLUTION
Avatar of mcrider
mcrider

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the points! Glad I could help!


Cheers!®©