Solved

vb.net shorten code

Posted on 2013-01-25
2
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

632 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