# Can some one please look at this code and see if there is a better way?

W

#### William Morgan

this is the code for a uuedecode function.. it works but is kinda slow
is there a better way in .net?

thanks for looking..

Private Function DecodeString(ByVal InString As String, ByVal Bytes As
Long) As String
Dim OutString As String
Dim i As Long
Dim x0, x1, x2 As Long 'These are the chars that will be
spit out
Dim y0, y1, y2, y3 As Long 'These are what we got in

Try
For i = 1 To Len(InString) Step 4
y0 = Asc(Mid(InString, i, 1)) 'Get 4 chars and put
into 'y's
y1 = Asc(Mid(InString, i + 1, 1))
y2 = Asc(Mid(InString, i + 2, 1))
y3 = Asc(Mid(InString, i + 3, 1))

If (y0 = 96) Then y0 = 32 'If char is 96 then set to 2
If (y1 = 96) Then y1 = 32
If (y2 = 96) Then y2 = 32
If (y3 = 96) Then y3 = 32

x0 = ((y0 - 32) * 4) + ((y1 - 32) \ 16) 'Calculate the
3 chars
x1 = ((y1 Mod 16) * 16) + ((y2 - 32) \ 4)
x2 = ((y2 Mod 4) * 64) + (y3 - 32)

OutString = OutString + Chr(x0) + Chr(x1) + Chr(x2)
Next i
If Len(OutString) > Bytes Then
DecodeString = Microsoft.VisualBasic.Left(OutString,
Bytes)

Else
DecodeString = OutString
End If
Catch ex As Exception
End Try
Return DecodeString
End Function

J

#### Jay B. Harlow [MVP - Outlook]

William,
A number of potential performance problems.

1. Use of Asc & Chr. Asc & Chr will cause the character to be encoded &
decoded from the current ANSI code page (as defined under Windows). I would
use AscW & ChrW and leave the characters as Unicode. I would ensure that
when I read the string it used the proper System.TextEncoding object to
ensure that the characters were all

2. Use of Mid, I would use String.Chars property instead, as you simply want
a single char (in sets of 4).

3. Use of string Concatenation, I would use a StringBuilder instead.

4. I would consider using the shift operators over multiple & divide.

5. Use of Long variables, I would use Integer (a 32-bit value) unless I
specifically needed a Long (a 64-bit value).

Something like (untested, syntax checked only):

Private Shared Function DecodeString(ByVal InString As String, ByVal
Bytes As Integer) As String
Dim OutString As New System.Text.StringBuilder(InString.Length * 3 \
4)
Dim x0, x1, x2 As Integer 'These are the chars that will be spit out
Dim y0, y1, y2, y3 As Integer 'These are what we got in

For i As Integer = 0 To Len(InString) - 1 Step 4
y0 = AscW(InString.Chars(i)) 'Get 4 chars and put into 'y's
y1 = AscW(InString.Chars(i + 1))
y2 = AscW(InString.Chars(i + 2))
y3 = AscW(InString.Chars(i + 3))

If (y0 = 96) Then y0 = 32 'If char is 96 then set to 2
If (y1 = 96) Then y1 = 32
If (y2 = 96) Then y2 = 32
If (y3 = 96) Then y3 = 32

x0 = ((y0 - 32) << 2) + ((y1 - 32) >> 4) 'Calculate the 3 chars

x1 = ((y1 Mod 16) << 4) + ((y2 - 32) >> 2)

x2 = ((y2 Mod 4) << 6) + (y3 - 32)

OutString.Append(ChrW(x0))
OutString.Append(ChrW(x1))
OutString.Append(ChrW(x2))
Next i
If OutString.Length > Bytes Then
OutString.Length = Bytes
End If
Return OutString.ToString()
End Function

Hope this helps
Jay

C

#### Cor Ligthert

Hi William,

In addition to the complete answer from Jay, I think that it will be good to
look as well to the "string.substring" it can be a very handy method in what
you are doing.

Cor

W

#### William Morgan

thanks jay your code does work much better.