[Tut]Hex > String, String > Hex

Posts 1–2 of 2 · Page 1 of 1
[Tut]Hex > String, String > Hex
I am writing a tut, Ill post it here, I have asked Blubb to add pictures and make it a tut for the VB section.


Here is the TUT

Convert Hex To String and back to Hex.

  • As Always Open Visual Basic .NET and follow along


  • Add a textbox, Label and 2 buttons to your form


  1. Button1 Text = "To Hex"
  2. Button2 Text = "To String"




  • Double Click Button One and add this code


Code:
 Dim str As String = TextBox1.Text
        Dim bytes() As Byte
        Dim hexn As System.Text.StringBuilder = New System.Text.StringBuilder

        bytes = System.Text.ASCIIEncoding.ASCII.GetBytes(str)

        For i As Integer = 0 To bytes.Length - 1
            hexn.Append(bytes(i).ToString("x"))
        Next

        Label1.Text = (hexN.ToString())
This code converts the text in textbox one to hex

  • Double Click Button two and add this code


Code:
Dim S As String = TextBox1.Text
        Dim EM As String = ""
        Dim i As Integer
        For i = 0 To S.Length - 2 Step 2
            EM = EM & Chr(CInt("&H" & S.Substring(i, 2)))
        Next
        Label1.Text = EM
Note: You are going to want to handle errors, If you input a string and try to convert it to a string it will error, so be sure you add error handling.

Use:

Code:
Try
- Your code here -

Catch ex as exception
msgbox(errortostring)
end try


This Code converts the text in textbox1 and converts it to a string

Hope This Helps

Tutorial by Nextgen1

Pictures and Error handling by me xD

So this was ng's tutorial. However I made it different, my method is way faster if you have tons of hex/string...

I had a .txt with a size of 4mb containg hex string. With nextgens version my program died after 60 seconds..My version needed 10 seconds to convert =D

Also my version is just using a website and filling in the data, clicking a button and getting the converted text again...Easy method huh :]

You need 2 Textboxes and a webbrowser. I'm not going to make a whole tut for that I'll just let you copy&paste mine...

Form1_load

Code:
web.Navigate("http://www.dolcevie.com/js/converter.html")

        Application.DoEvents()

        Do While web.ReadyState <> WebBrowserReadyState.Complete

            Application.DoEvents()

        Loop
Hex > string button

Code:
  web.Document.GetElementById("hex").InnerText = richtextbox1.Text

        Application.DoEvents()

        Do While web.ReadyState <> WebBrowserReadyState.Complete

            Application.DoEvents()

        Loop

        web.Document.GetElementById("b13").Focus()

        Application.DoEvents()
        SendKeys.Send(" ")

        Application.DoEvents()


        Do While web.ReadyState <> WebBrowserReadyState.Complete

            Application.DoEvents()

        Loop

        rb2.Text = web.Document.GetElementById("ascii").InnerText

        Dim s As String = rb2.Text
        Dim s2 As String
        s2 = s.Replace("?", "")

        Application.DoEvents()

        richtextbox2.Text = s2
You gotta find out the string > hex button on your own, isn't hard :]
Looks Good, And Thanks for the creds
Posts 1–2 of 2 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Talk with us