Solved

vb.net shorten code

Posted on 2013-01-25
2
323 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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

8 Experts available now in Live!

Get 1:1 Help Now