We help IT Professionals succeed at work.

VB.NET HEX to STRING

Hi there,

I have this hex string:
 
001F33EE593F988B5D984370080045000034447740006D061117607F95CAC0A801442B175B6DD9DE7A70EEA234548010010212F400000101080A05FAA8850052FDF4
001F33EE593F988B5D98437008004500003446DE40006D060EB0607F95CAC0A801442B175B6DD9DE7A70EEA234548011010212E900000101080A05FAA88F0052FDF4

Open in new window


and I converted it online here:
http://www.string-functions.com/hex-string.aspx

and got this:
 
¿3îY?˜‹]˜Cp¿E¿¿4Dw@¿m`•ÊÀ¨D+[mÙÞzpî¢4T€ô¿¿
ú¨…¿Rýô?¿3îY?˜‹]˜Cp¿E¿¿4FÞ@¿m°`•ÊÀ¨D+[mÙÞzpî¢4T€é¿¿
ú¨¿Rýô

Open in new window


Now how do I get the same output using vb.net?
Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2014

Commented:
That looks a byte array to me

Try

Dim str As String = System.Text.Encoding.Ascii.GetString(yourbytearray)
Most Valuable Expert 2011
Top Expert 2015

Commented:
See if this fits your needs:

Dim interval As Integer = 2
Dim result As New Text.StringBuilder()

For i As Integer = 0 To source.Length - 1 Step interval
    Dim hex As String = source.Substring(i, interval)
    Dim c As Char = Convert.ToChar(Convert.ToInt32(hex, 16))

    result.Append(c)
Next

' result.ToString() will give the resulting string

Open in new window

Commented:
You can try this for both cases:


Imports System.Text

Private Function EncodeHexToString(ByVal strHexString As String) As String

	Dim intHexStrLen As Integer = strHexString.Length
	Dim intCicle As Integer = 0
	Dim sbEncoded As New StringBuilder(intHexStrLen * 2)
	Dim bArrBytes() As Byte = System.Text.Encoding.ASCII.GetBytes(strHexString)

	For intCicle = 0 To bArrBytes.Length - 1
		sbEncoded.AppendFormat("{0:X2}", bArrBytes(intCicle))
	Next

	Return sbEncodeed.ToString()

End Function

Private Function DecodeHexString(ByVal strEncodedString As String) As String

	Dim intEncodedString As Integer = strEncodedString.Length
	Dim intCicle As Integer = 0
	Dim sbDecoded As New StringBuilder(CType(intEncodedString / 2, Integer))
	
	Try
		For intCicle = 0 To strEncodedString.Length - 1 Step 2
			sbDecoded.Append(Convert.ToChar(Byte.Parse(sText.Substring(intCicle, 2), Globalization.NumberStyles.HexNumber)))
		Next
	Catch ex As Exception
		Return ""
	End Try
	Return sbDecoded.ToString()

End Function

' so you can call your code like:
' Dim strYourHexString = "something Hex"
' Dim strYourEncodedHexString As String = EncodeHexToString(strYourHexString)
' MsgBox(strYourEncodedHexString)
' Dim strYourDecodedHexString As String = DecodeHexString(strEncodingYourHexString)
' MessageBox.Show(strYourDecodedHexString)

Open in new window

Author

Commented:
None of these methods work. All of them just output nothing.

Author

Commented:
I'm using PacketX to capture packets.

http://www.beesync.com/packetx/docs/html/interfaceIPktXPacket.html#9e4960cc81aa11e52018e36a126cd44e

 
Private Sub AxPacketXCtrl1_OnPacket(ByVal sender As Object, ByVal e As AxPacketXLib._IPktXPacketXCtrlEvents_OnPacketEvent) Handles AxPacketXCtrl1.OnPacket
        oPacketColl.Add(e.pPacket)
        If e.pPacket.SourceIpAddress = Login.serverip Then
            Dim hexstring As String = BytesToHeX(e.pPacket.DataArray)
            DoLog(hexstring)
        End If
    End Sub

    Private Shared Function BytesToHeX(ByVal bytes As Byte()) As String
        Return String.Join(String.Empty, Array.ConvertAll(bytes, Function(b) b.ToString("X2")))
    End Function

Open in new window

Author

Commented:
It is definitely a hex string, because I can see it the same way in wireshark too. The only problem is that I need to decode it into the string in the same way as the website does.

Let's say this hex code here:

 
001F33EE593F988B5D9843700800450000A83C9640006D061884607F95CAC0A801442B1708BB3A357A0BCB56CD5A8018010248F300000101080A063386A000027E1400060B00000000000049070D0163616C64617661727C6772696D6D7363726F7373696E6700317C320061707C73647C62647C62707C6172005553577C5553457C455500004742000A010A020A0503030408060A00036A0000001A680066A900004E413A31383438337C5345413A32343838337C00

Open in new window


Should be this:

 
¿3îY?˜‹]˜Cp¿E¿¿¨<–@¿m„`•ÊÀ¨D+»:5zËVÍZ€Hó¿¿
3† ¿~¿¿¿¿¿¿¿I
caldavar|grimmscrossing¿1|2¿ap|sd|bd|bp|ar¿USW|USE|EU¿¿GB¿



¿j¿¿¿h¿f©¿¿NA:18483|SEA:24883|¿

Open in new window


Converted with: http://www.string-functions.com/hex-string.aspx

Author

Commented:
This PHP code gives me correct answer:

 
function hexToStr($hex)
{
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}

Open in new window

Author

Commented:
I think that problem may not be with code. I tried to just send request to the .php page with that output on the page, and got nothing in result, just a blank page. Probably vb.net doesn't want to display those characters ?

Author

Commented:
Or maybe the way vb.net parses characters.

Author

Commented:
I think that the problem is that in bytearray there are non unicode characters. When they are converted to hex, vb.net just won't deal with them... Maybe there is a way to get the same output without converting to hex and just get it from bytearray?
Most Valuable Expert 2012
Top Expert 2014

Commented:
>Dim hexstring As String = BytesToHeX(e.pPacket.DataArray)


You are receiving a byte array which you can directly convert to string. Why are you converting it to hex?


>I think that problem may not be with code. I tried to just send request to the .php page with that output on the page, and got nothing in result, just a blank page. Probably vb.net doesn't want to display those characters ?

>Or maybe the way vb.net parses characters.

>I think that the problem is that in bytearray there are non unicode characters. When they are converted to hex, vb.net just won't deal with them...

So you do not think the problem could be with the way you are converting byte array to hex?

Author

Commented:
Maybe there is a way to display only unicode characters out of all of the characters?

Author

Commented:
Yes because I couldn't seem to convert the bytearray to string at all.

Author

Commented:
I always got a blank output. If you know how to convert bytearray to string I'd me more than happy to use that.
Most Valuable Expert 2012
Top Expert 2014

Commented:
What code did you try to convert byte array to string?

Author

Commented:
Found some one the internet, although they are giving the hex code.

 
Public Shared Function ByteArrayToString(ba As Byte()) As String
	Dim hex As String = BitConverter.ToString(ba)
	Return hex.Replace("-", "")
End Function

Open in new window


 
Public Shared Function ByteArrayToString(ByVal ba As Byte()) As String
        Dim hex As New StringBuilder(ba.Length * 2)
        For Each b As Byte In ba
            hex.AppendFormat("{0:x2}", b)
        Next
        Return hex.ToString()
    End Function

Open in new window

Most Valuable Expert 2012
Top Expert 2014

Commented:
That's not byte array to string.

Let's you have your bytes in byteData variable then

Dim str as String = System.Text.Encoding.Ascii.GetString(byteData)

Author

Commented:
Dim str as String = System.Text.Encoding.Ascii.GetString(byteData)

gives nothing - empty result.

Author

Commented:
           Dim str2 As String = System.Text.Encoding.Unicode.GetString(e.pPacket.DataArray)
            DoLog(str2)

gives me this:

¿¿¿¿¿¿E¿¿@¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿E¿¿@¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿

Author

Commented:
hmm

 
¿¿¿¿¿¿E¿¿@¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿E¿¿@¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿

Open in new window

Author

Commented:
Weird... It gives some Chinese - like symbols.

Author

Commented:
System.Text.Encoding.Unicode.GetString(e.pPacket.DataArray)
Most Valuable Expert 2012
Top Expert 2014

Commented:
What encoding is used on the bytes received?
Most Valuable Expert 2011
Top Expert 2015

Commented:
Note also that the font you are using to display the characters may not support the characters you are decoding. You'll want to ensure you are using a Unicode font to display the converted data.

Author

Commented:
I'm not sure... Probably you want to look here:

http://www.beesync.com/packetx/docs/html/interfaceIPktXPacket.html

Maybe you 'll understand more

Author

Commented:
I tried to make a php file, that converts data from hex to string. Page gives a correct output, however when I tried vb.net to get the output of the page, it displays nothing.

Works fine with normal text output.

Author

Commented:
I tried a page to echo "FFFF" each time request is sent to it. That worked.

When I changed back for a .php page to display the converted hex code, my program displayed nothing.

There is definitely some problem with those characters.

Author

Commented:
When I just typed "3îY?˜‹]˜CpE4k4@mêY`•ÊÀ¨D+ Q+Z¬/÷€R 4ä/Û¤" as string, it displayed it... so where the heck is a problem?
Most Valuable Expert 2012
Top Expert 2014

Commented:
I found below code (converted from C#) in one of their examples



Private Shared Sub PrintData(aPacket As Packet)
	Dim sData As String = "----------------------- Packet Data -----------------------" & vbLf

	Dim nPosition As Integer = 0, nColumns As Integer = 16
	For i As Integer = 0 To aPacket.DataArray.Length - 1
		If nPosition = 8 Then
			sData += " "
		End If

		If nPosition >= nColumns Then
			nPosition = 1
			sData += vbLf
		Else
			nPosition += 1
		End If

		Dim nByte As Byte = CByte(aPacket.DataArray.GetValue(i))
		If nByte < 16 Then
			sData += "0"
		End If

		sData += nByte.ToString("X", oCulture.NumberFormat) + " "
	Next
	Console.WriteLine(sData)
End Sub

Open in new window

Author

Commented:
----------------------- Packet Data -----------------------
00 1F 33 EE 59 3F 98 8B  5D 98 43 70 08 00 45 00
00 A8 27 15 40 00 6D 06  2E 05 60 7F 95 CA C0 A8
01 44 2B 17 2F 56 77 59  AA F3 F9 42 A9 64 80 18
01 02 08 15 00 00 01 01  08 0A 06 49 DD 9A 00 18
D5 0E 00 06 0B 00 00 00  00 00 00 49 07 0D 01 63
61 6C 64 61 76 61 72 7C  67 72 69 6D 6D 73 63 72
6F 73 73 69 6E 67 00 31  7C 32 00 61 70 7C 73 64
7C 62 64 7C 62 70 7C 61  72 00 55 53 57 7C 55 53
45 7C 45 55 00 00 47 42  00 0A 00 0A 01 0A 05 02
02 03 04 05 0A 00 03 6A  00 00 00 1A 68 00 6D C9
00 00 4E 41 3A 32 36 39  36 31 7C 53 45 41 3A 32
34 36 30 34 7C 00

Author

Commented:
Converted using: http://www.string-functions.com/hex-string.aspx

Converted version looks like this:

 
¿3îY?˜‹]˜Cp¿E¿?¿¨'@¿m.`•ÊÀ¨?D+/VwYªóùB©d€?¿¿
IÝš¿?Õ¿¿¿¿¿¿¿I
c?aldavar|grimmscr?ossing¿1|2¿ap|sd?|bd|bp|ar¿USW|US?E|EU¿¿GB¿
¿

?
¿j¿¿¿h¿mÉ?¿¿NA:26961|SEA:2?4604|¿

Open in new window


How do I get it to be this way?
Most Valuable Expert 2012
Top Expert 2014

Commented:
What are you expecting?

Author

Commented:
I want to convert this:

 
----------------------- Packet Data -----------------------
00 1F 33 EE 59 3F 98 8B  5D 98 43 70 08 00 45 00 
00 A8 27 15 40 00 6D 06  2E 05 60 7F 95 CA C0 A8 
01 44 2B 17 2F 56 77 59  AA F3 F9 42 A9 64 80 18 
01 02 08 15 00 00 01 01  08 0A 06 49 DD 9A 00 18 
D5 0E 00 06 0B 00 00 00  00 00 00 49 07 0D 01 63 
61 6C 64 61 76 61 72 7C  67 72 69 6D 6D 73 63 72 
6F 73 73 69 6E 67 00 31  7C 32 00 61 70 7C 73 64 
7C 62 64 7C 62 70 7C 61  72 00 55 53 57 7C 55 53 
45 7C 45 55 00 00 47 42  00 0A 00 0A 01 0A 05 02 
02 03 04 05 0A 00 03 6A  00 00 00 1A 68 00 6D C9 
00 00 4E 41 3A 32 36 39  36 31 7C 53 45 41 3A 32 
34 36 30 34 7C 00

Open in new window


Into this:

 
¿3îY?˜‹]˜Cp¿E¿?¿¨'@¿m.`•ÊÀ¨?D+/VwYªóùB©d€?¿¿
IÝš¿?Õ¿¿¿¿¿¿¿I
c?aldavar|grimmscr?ossing¿1|2¿ap|sd?|bd|bp|ar¿USW|US?E|EU¿¿GB¿
¿

?
¿j¿¿¿h¿mÉ?¿¿NA:26961|SEA:2?4604|¿

Open in new window


Using VB.NET not online converter.
Most Valuable Expert 2012
Top Expert 2014
Commented:
What output do you get with


Private Shared Sub PrintData(aPacket As Packet)
	Dim sData As String = "----------------------- Packet Data -----------------------" & vbLf
        sData &= System.Text.Encoding.Ascii.GetString(aPacket.DataArray)
   	Console.WriteLine(sData)
End Sub

Open in new window

Author

Commented:
Private Sub AxPacketXCtrl1_OnPacket(ByVal sender As Object, ByVal e As AxPacketXLib._IPktXPacketXCtrlEvents_OnPacketEvent) Handles PacketGatherer.OnPacket
        oPacketColl.Add(e.pPacket)
        If e.pPacket.SourceIpAddress = Login.serverip Then
            Dim hexstring As String = BytesToHeX(e.pPacket.DataArray)
            Dim sText As String = hexstring
            Dim bytes() As Byte = HexsToArray(sText)
            Dim chars() As Char = ASCIIEncoding.Default.GetChars(bytes)
            Dim str As String = New String(chars)
            Dim sw As New StreamWriter("C:\1.txt")
            sw.WriteLine(str)
            sw.Dispose()
            sw.Close()
            DoLog(str)
        End If
    End Sub

    Function HexsToArray(ByVal sHexString As String) As Byte()
        Dim ra(sHexString.Length / 2 - 1) As Byte
        For i As Int32 = 0 To sHexString.Length - 1 Step 2
            ra(i / 2) = Byte.Parse(sHexString.Substring(i, 2), _
                  NumberStyles.HexNumber)
        Next i
        Return ra
    End Function

    Private Shared Function BytesToHeX(ByVal bytes As Byte()) As String
        Return String.Join(String.Empty, Array.ConvertAll(bytes, Function(b) b.ToString("X2")))
    End Function

Open in new window


Now this is the fun part.

With the code above, into the text file, he write the stuff I need exactly how I need it. But when I try to insert the same string into the textbox, it outputs nothing... Why?
Most Valuable Expert 2011
Top Expert 2015

Commented:
What font are you using on your TextBox?

Author

Commented:
Arial