Solved

vb.net shorten code

Posted on 2013-01-25
2
327 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c# DateTime Format validation 4 68
Making exceptions for ValidationAttribute 2 24
parsing question on dynamic concatenation 3 34
Return array 3 20
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

756 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