none
Convert Hex to ASCII RRS feed

  • Question

  • Hello, how can i convert from Hex decimal to ascii ?

    i have 2 textbox (inputhex , outputascii)
    i have 1 button (btnconvert)

    example: hex data: 2B4E2B532D4D
    will reply me: +N+S-M

    thanks alote .


    • Edited by Yaki159 Sunday, July 5, 2020 7:33 PM
    Sunday, July 5, 2020 7:32 PM

Answers

  • Hi, thank you.
    have error with the bold line .
    and please check if all the other is currect

    Public Class Form1

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        End Sub

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim s As String
            Dim res As New List(Of String)
            s = inputhex.Text
            For i As Integer = 0 To s.Length - 1 Step 2
                res.Add(
          ChrW(Convert.ToInt32(s.Substring(i, 2), 16)))
            Next
            outputascii.Text = (inputhex.ToString())
        End Sub

        Private Function ChrW(p As Object) As String
            Throw New NotImplementedException()
        End Function
    End Class

    Hi

    Try this version. This is a stand alone example, and needs TextBox1, TextBox2 and Button1 on Form1.

    ' Form1 with TextBox1,
    ' TextBox2 and Button1
    Option Strict On
    Option Explicit Off
    Public Class Form1
      Function GetA(s As String) As String
        Dim res As String = Nothing
        For i As Integer = 0 To s.Length - 1 Step 2
          res &=
          ChrW(Convert.ToInt32(s.Substring(i, 2), 16))
        Next
        Return res
      End Function
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox2.Text = GetA(TextBox1.Text)
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Yaki159 Monday, July 6, 2020 5:15 PM
    Monday, July 6, 2020 1:54 PM

All replies

  • Hi

    Have a go with this - make it to suit your code.

        Dim s As String = "2B4E2B532D4D"
        Dim res As New List(Of String)
        For i As Integer = 0 To s.Length - 1 Step 2
          res.Add(
          ChrW(Convert.ToInt32(s.Substring(i, 2), 16)))
        Next
        ' res = "+N+S-M"
    


    Regards Les, Livingston, Scotland

    Sunday, July 5, 2020 8:03 PM
  • Hi, thank you.
    have error with the bold line .
    and please check if all the other is currect

    Public Class Form1

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        End Sub

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim s As String
            Dim res As New List(Of String)
            s = inputhex.Text
            For i As Integer = 0 To s.Length - 1 Step 2
                res.Add(
          ChrW(Convert.ToInt32(s.Substring(i, 2), 16)))
            Next
            outputascii.Text = (inputhex.ToString())
        End Sub

        Private Function ChrW(p As Object) As String
            Throw New NotImplementedException()
        End Function
    End Class

    • Edited by Yaki159 Monday, July 6, 2020 11:54 AM
    Monday, July 6, 2020 5:29 AM
  • Hi, thank you.
    have error with the bold line .
    and please check if all the other is currect

    Public Class Form1

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        End Sub

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim s As String
            Dim res As New List(Of String)
            s = inputhex.Text
            For i As Integer = 0 To s.Length - 1 Step 2
                res.Add(
          ChrW(Convert.ToInt32(s.Substring(i, 2), 16)))
            Next
            outputascii.Text = (inputhex.ToString())
        End Sub

        Private Function ChrW(p As Object) As String
            Throw New NotImplementedException()
        End Function
    End Class

    Here is your error reason


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, July 6, 2020 1:09 PM
    Moderator
  • Hi, thank you.
    have error with the bold line .
    and please check if all the other is currect

    Public Class Form1

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        End Sub

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim s As String
            Dim res As New List(Of String)
            s = inputhex.Text
            For i As Integer = 0 To s.Length - 1 Step 2
                res.Add(
          ChrW(Convert.ToInt32(s.Substring(i, 2), 16)))
            Next
            outputascii.Text = (inputhex.ToString())
        End Sub

        Private Function ChrW(p As Object) As String
            Throw New NotImplementedException()
        End Function
    End Class

    Hi

    Try this version. This is a stand alone example, and needs TextBox1, TextBox2 and Button1 on Form1.

    ' Form1 with TextBox1,
    ' TextBox2 and Button1
    Option Strict On
    Option Explicit Off
    Public Class Form1
      Function GetA(s As String) As String
        Dim res As String = Nothing
        For i As Integer = 0 To s.Length - 1 Step 2
          res &=
          ChrW(Convert.ToInt32(s.Substring(i, 2), 16))
        Next
        Return res
      End Function
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox2.Text = GetA(TextBox1.Text)
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Yaki159 Monday, July 6, 2020 5:15 PM
    Monday, July 6, 2020 1:54 PM

  • have error with the bold line .

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

            Dim s As String
            Dim res As New List(Of String)
            s = inputhex.Text
            For i As Integer = 0 To s.Length - 1 Step 2
                res.Add(
          ChrW(Convert.ToInt32(s.Substring(i, 2), 16)))
            Next
            outputascii.Text = (inputhex.ToString())
        End Sub

        Private Function ChrW(p As Object) As String
            Throw New NotImplementedException()
        End Function


    When you get errors from code you should always show those errors.
    Don't make us guess or have to try to generate errors randomly.

    Why do you have the function ChrW defined in your code? Get rid of it.

    ChrW is defined by VB:

    Strings.ChrW(Int32) Method
    https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.strings.chrw?view=netcore-3.1

    - Wayne

    Monday, July 6, 2020 2:48 PM
  • ASCII text encoding uses fixed 1 byte for each character.

    UTF-8 text encoding uses variable number of bytes for each character. This requires delimiter between each hex number.

    How to Convert Hex to Text
    Convert hex ASCII code to text:

    Get hex byte
    Convert hex byte to decimal
    Get character of ASCII code from ASCII table
    Continue with next byte
    Example
    Convert "50 6C 61 6E 74 20 74 72 65 65 73" hex ASCII code to text:

    Solution:

    Use ASCII table to get character from ASCII code.

    5016 = 5×161+0×160 = 80+0 = 80 => "P"

    6C16 = 6×161+12×160 = 96+12 = 108 => "l"

    6116 = 6×161+1×160 = 96+1 = 97 => "a"



    For all the hex bytes you should get the text:

    "Plant trees"

    How to convert Hex to Text?
    Get hex byte code
    Convert hex byte to decimal
    Get character of decimal ASCII code from ASCII table
    Continue with next hex byte
    How to use Hex to ASCII Text converter?
    Paste hex byte codes in input text box.
    Select character encoding type.
    Press the Convert button.
    How to convert Hex code to English?
    Get hex byte code
    Convert hex byte to decimal
    Get english letter of decimal ASCII code from ASCII table
    Continue with next hex byte
    How to convert 41 hex to text?
    Use ASCII table:
    41 = 4×16^1+1×16^0 = 64+1 = 65 = 'A' character

    How to convert 30 hex to text?
    Use ASCII table:
    30 = 3×16^1+0×16^0 = 48 = '0' character

    "name": "Hex to ASCII Text Converter", "mainEntity": [{ "@type": "Question", "name": "How to convert Hex to Text?", "acceptedAnswer": { "@type": "Answer", "text": "
    Get hex byte code
    Convert hex byte to decimal
    Get character of decimal ASCII code from ASCII table
    Continue with next hex byte
    Read more

    " } }, { "@type": "Question", "name": "How to use Hex to ASCII Text converter?", "acceptedAnswer": { "@type": "Answer", "text": "
    Paste hex byte codes in input text box.
    Select character encoding type.
    Press the Convert button.
    Read more

    " } }, { "@type": "Question", "name": "How to convert Hex code to English?", "acceptedAnswer": { "@type": "Answer", "text": "
    Get hex byte code
    Convert hex byte to decimal
    Get english letter of decimal ASCII code from ASCII table
    Continue with next hex byte
    Read more

    " } }, { "@type": "Question", "name": "How to convert 41 hex to text?", "acceptedAnswer": { "@type": "Answer", "text": "Use ASCII table:
    41 = 4×16^1+1×16^0 = 64+1 = 65 = 'A' character
    Read more

    " } }, { "@type": "Question", "name": "How to convert 30 hex to text?", "acceptedAnswer": { "@type": "Answer", "text": "Use ASCII table:
    30 = 3×16^1+0×16^0 = 48 = '0' character
    Monday, July 6, 2020 4:09 PM
  • It is Work!!!

    Thank you :)

    Monday, July 6, 2020 5:16 PM
  • Wayne,

    The code is perfectly well. This is a VB forum, not F# or something. 

    Bellow in another reply  you see what you would have to do if you are not using VB.

    I changed the code from Les a little bit, (the stringbuilder) but that is just a detail (in fact the predecessor of the List(of String)

      Function GetA(s As String) As String
            Dim res As New Text.StringBuilder
            For i As Integer = 0 To s.Length - 1 Step 2
                res.Append(ChrW(Convert.ToInt32(s.Substring(i, 2), 16)))
            Next
            Return res.ToString
        End Function
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TextBox2.Text = GetA(TextBox1.Text)
        End Sub

    The error is probably thrown because there was a wrong character or an odd range entered. 

    (Les it took me a while, because I had problems with Visual Studio, otherwise I had proposed your reply as answers)


    Success
    Cor


    Monday, July 6, 2020 5:20 PM
  • Wayne,

    The code is perfectly well. This is a VB forum, not F# or something.  

    Cor


    What on earth are you talking about? It should be intuitively obvious from my
    reply that I am aware that this is VB. That's why I said: 
    "ChrW is defined by VB:" and provided a link.

    As Karen correctly noted the issue is because the OP included this code in the
    program:

    Private Function ChrW(p As Object) As String
        Throw New NotImplementedException()
    End Function
    

    That function gets invoked instead of the VB function/method that should be
    called, resulting in a runtime exception being thrown from the above function.

    I supplemented her reply by advising the OP to remove that code, and 
    explained why.

    Perhaps my reply would not be subject to misinterpretation if instead of
    saying:

    "Why do you have the function ChrW in your code? Get rid of it."

    I had said:

    "Why do you have the function ChrW defined in your code? Get rid of it."

    - Wayne

    Monday, July 6, 2020 5:44 PM