Solved

vb.net shorten code

Posted on 2013-01-25
2
324 Views
Last Modified: 2013-01-26
hello there,
this is working perfectly fine but I would like to know if its possible to get the same results with a shorter code..

        Dim enc2 As New System.Text.UTF8Encoding()
        Dim enc3 As String = enc2.GetString(bBuffer)
        Dim abData() As Byte
        abData = System.Text.Encoding.Default.GetBytes(enc3)
        Dim i As Long
        enc3 = ""
        For i = 0 To UBound(abData)
            If Hex(abData(i)).Length = 1 Then
                enc3 += "0" & Hex(abData(i)) & " "
            Else
                enc3 += Hex(abData(i)) & " "
            End If
        Next

Open in new window

0
Comment
Question by:XK8ER
2 Comments
 
LVL 7

Accepted Solution

by:
Element1910 earned 250 total points
ID: 38820931
You did a pretty good job of getting it pretty much down to as little code as possible. The only thing I notice is that you can get rid of Dim i as Long and combine it into the for loop, like so:
Dim enc2 As New System.Text.UTF8Encoding()
Dim enc3 As String = enc2.GetString(bBuffer)
Dim abData As Byte() = Nothing
abData = System.Text.Encoding.[Default].GetBytes(enc3)
enc3 = ""
For i As Long = 0 To Information.UBound(abData)
	If Hex(abData(i)).Length = 1 Then
		enc3 += "0" & Hex(abData(i)) & " "
	Else
		enc3 += Hex(abData(i)) & " "
	End If
Next

Open in new window

0
 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 250 total points
ID: 38820983
Only half tested but give this a go.

The bit inside the for-next can be done with:
enc3 += abData(i).ToString("X2") & " "

Open in new window

But for the whole thing in one try this:
enc3 = BitConverter.ToString(abData)

Open in new window

You could replace the dashes with spaces of course. Or even use that on bBuffer but I'm not sure what you're doing with the UTF conversion...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now