473,513 Members | 2,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Nov 20 '05 #1
3 1280
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

"William Morgan" <wm*****@madisoncounty.net> wrote in message
news:ku********************************@4ax.com...
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

Nov 20 '05 #2
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
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

Nov 20 '05 #3
thanks jay your code does work much better.

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
4499
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
1
2589
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for...
2
2042
by: Daniel | last post by:
I'm new to .Net and all of its abilities so I hope this makes sense. Basically I'm confused on when is the appropriate time to use web forms controls vs. regular HTML. For example in ASP...
59
4931
by: Alan Silver | last post by:
Hello, This is NOT a troll, it's a genuine question. Please read right through to see why. I have been using Vusual Basic and Classic ASP for some years, and have now started looking at...
21
1941
by: salad | last post by:
Thanks for reading this post and wasting your time. I was thinking about writing about the PCDatasheet vs The Pussyfarts war. The pussyfarts, as near as I can tell, have little to offer this...
1
9587
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
5
3341
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
29
2871
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
7
3262
by: Protected | last post by:
Hello. I'm a complete newbie trying to learn Python. I decided to try some Tkinter examples, including the one from the library reference, but they don't seem to do anything! Shouldn't there be,...
112
4585
by: Prisoner at War | last post by:
Friends, your opinions and advice, please: I have a very simple JavaScript image-swap which works on my end but when uploaded to my host at http://buildit.sitesell.com/sunnyside.html does not...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.