none
Making a HEX to ASCII converter in Visual Basic 2010

    Question

  • Hello people of the Visual Studio forums.

    I have a question, I am making a ASCII to Hex converter in Visual Basic 2010. Someone helped me make the code for ASCII to Hex awhile back, but now I want to make it also Hex into ASCII, but in the same fashion as the ASCII to HEX code below.  If someone could help me with that I would greatly appreciate it. and I apologize if this is the wrong section, it wouldn't let me make a thread in the Visual Basic category for some reason.

    Private Sub inputASCII_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles inputASCII.TextChanged
            Dim str As String
            Dim byteArray() As Byte
            Dim asciihexOUT As System.Text.StringBuilder = New System.Text.StringBuilder
    
    
            str = inputASCII.Text
    
    
            byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(str)
    
    
            For i As Integer = 0 To byteArray.Length - 1
                asciihexOUT.Append(byteArray(i).ToString("X") + (" "))
            Next
    
    
            outputHEX.Text = (asciihexOUT.ToString())
        End Sub
    Sunday, January 13, 2013 3:11 AM

Answers

  • add another textbox (OutputASCII)

        Private Sub OutputHEX_TextChanged(sender As System.Object, e As System.EventArgs) Handles OutputHEX.TextChanged
            Dim asciiOUT As System.Text.StringBuilder = New System.Text.StringBuilder
            OutputASCII.Clear()
            Dim Bytes As New List(Of String)
            Bytes.AddRange(OutputHEX.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries))
            For Each s As String In Bytes
                If s.Length = 2 Then
                    asciiOUT.Append(Chr("&H" & s))
                End If
            Next
            OutputASCII.Text = asciiOUT.ToString
        End Sub
    

    • Marked as answer by Glad0s Sunday, January 13, 2013 7:30 PM
    Sunday, January 13, 2013 5:05 PM

All replies

  • Byte.Parse and ToString are the easiest ways to convert a hex string to/from a byte:

    Dim hexNumber As String = "FF"
    Dim hexValue As Byte = Byte.Parse(hexNumber, Globalization.NumberStyles.AllowHexSpecifier)
    hexNumber = hexValue.ToString("X2")
    

    Depending on how you've formatted your display, you just need to ensure that you move through two hex characters at a time and then you can parse them back into bytes.  You continue to use the ASCIIEncoding to convert the bytes to/from characters.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Sunday, January 13, 2013 3:52 AM
    Moderator
  • That helps me a little, but the main thing I was looking for was to reverse my code.

    Instead of how it is originally ASCII to HEX, I would want to make it HEX to ASCII

    plus more code.

    2 Textboxes (name one inputASCII, and the other outputHEX)
    2 Buttons (name one btnClear, and the other btnCopy

    Public Class Form1
    
        Private Sub inputASCII_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles inputASCII.TextChanged
            Dim str As String
            Dim byteArray() As Byte
            Dim asciihexOUT As System.Text.StringBuilder = New System.Text.StringBuilder
    
    
            str = inputASCII.Text
    
    
            byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(str)
    
    
            For i As Integer = 0 To byteArray.Length - 1
                asciihexOUT.Append(byteArray(i).ToString("X") + (" "))
            Next
    
    
            outputHEX.Text = (asciihexOUT.ToString())
        End Sub
    
    
        Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
            inputASCII.Text = Nothing
        End Sub
    
    
        Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
            Clipboard.SetText(outputHEX.Text)
        End Sub
    End Class
    Sorry I would show a picture of my Display but it wont let me put images
    Sunday, January 13, 2013 4:17 AM
  • Yea little bit problemfull, ASCII and Hex are apples and pears and that cannot be converted.

    ASCII is the digital code representation of characters, which mostly is represented in Decimal, Octal, or Hexadecimal format. But it is always the same.

    If you want to see the Charachter representation of a Hex expression then it is simply something like this.

            Dim a As Byte = 59
            Dim b = ChrW(a) 'makes a charachter a
            Dim c = Hex(a)  'makes a string "3B"


    Success
    Cor

    Sunday, January 13, 2013 11:23 AM
  • Glados, I have code that is as follows...

        Private Sub ConvertToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ConvertToolStripMenuItem.Click
            Dim HexData As String = ""
            For Each Character As Char In TextBox1.Text
                If Not HexData = "" Then
                    HexData += " "
                End If
                HexData += Hex(Asc(Character))
            Next
            TextBox1.Text = HexData
        End Sub
    
        Private Sub ReconvertToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ReconvertToolStripMenuItem.Click
            Dim StringData As String = ""
            For Each HexCharacter As String In Split(TextBox1.Text)
                StringData += Chr(Byte.Parse(HexCharacter, Globalization.NumberStyles.AllowHexSpecifier))
            Next
            TextBox1.Text = StringData
        End Sub

    Now, the convert button converts text from a textbox to hex, using a space as a separator character just to space things away, and the reconvert converts the hex data back into the text you had typed originally...although it overwrites the textbox each time to display between text & hex.  You should be able to adapt it to whatever you need to do.

    Sunday, January 13, 2013 4:48 PM
  • add another textbox (OutputASCII)

        Private Sub OutputHEX_TextChanged(sender As System.Object, e As System.EventArgs) Handles OutputHEX.TextChanged
            Dim asciiOUT As System.Text.StringBuilder = New System.Text.StringBuilder
            OutputASCII.Clear()
            Dim Bytes As New List(Of String)
            Bytes.AddRange(OutputHEX.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries))
            For Each s As String In Bytes
                If s.Length = 2 Then
                    asciiOUT.Append(Chr("&H" & s))
                End If
            Next
            OutputASCII.Text = asciiOUT.ToString
        End Sub
    

    • Marked as answer by Glad0s Sunday, January 13, 2013 7:30 PM
    Sunday, January 13, 2013 5:05 PM
  • add another textbox (OutputASCII)

        Private Sub OutputHEX_TextChanged(sender As System.Object, e As System.EventArgs) Handles OutputHEX.TextChanged
            Dim asciiOUT As System.Text.StringBuilder = New System.Text.StringBuilder
            OutputASCII.Clear()
            Dim Bytes As New List(Of String)
            Bytes.AddRange(OutputHEX.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries))
            For Each s As String In Bytes
                If s.Length = 2 Then
                    asciiOUT.Append(Chr("&H" & s))
                End If
            Next
            OutputASCII.Text = asciiOUT.ToString
        End Sub
    

    I would like to thank everyone for there contributions to helpin me, but this is what I was really looking for, though the other responses did help me gain a better understanding of what I was doing.  Thank you all very much.

    Hope to see you all again, as I plan to check out this forum more frequently.

    Sunday, January 13, 2013 7:30 PM