Asking help Ascii to Hex converter

Aeros05 21 Reputation points
2021-06-19T03:25:29.083+00:00

Hi all
Good day.

Asking for your help

I will create an app that converts Ascii to hex

-2 textbox ( inputascii value & outputhex value)
-1 button (Convert)

Wish you can help me.Thank you

Developer technologies | VB
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-06-19T10:51:16.51+00:00

    Try the following.

    Imports System.Text  
    Public Class Converters  
        Public Shared Function ToHexString(sender As String) As String  
            Dim sb = New StringBuilder()  
      
            Dim bytes = Encoding.Unicode.GetBytes(sender)  
            For Each currentByte In bytes  
                sb.Append(currentByte.ToString("X2"))  
            Next  
      
            Return sb.ToString()  
      
        End Function  
      
        Public Shared Function FromHexString(sender As String) As String  
            Dim bytes = New Byte((sender.Length \ 2) - 1) {}  
      
            For index = 0 To bytes.Length - 1  
                bytes(index) = Convert.ToByte(sender.Substring(index * 2, 2), 16)  
            Next  
      
            Return Encoding.Unicode.GetString(bytes)  
      
        End Function  
    End Class  
    

    Test

    Dim value = ""  
    Dim hexValue = ""  
      
    Dim list = new List(Of String) From {"Hello world", "This is a test", "123"}  
    For Each item As String In list  
        hexValue = Converters.ToHexString(item)  
        value = Converters.FromHexString(hexValue)  
        Debug.WriteLine($"{hexValue} -> {value}")  
    Next  
    

    107235-hex.jpg

    1 person found this answer helpful.
    0 comments No comments

  2. Castorix31 90,686 Reputation points
    2021-06-19T09:05:23.953+00:00
    0 comments No comments

  3. WayneAKing 4,931 Reputation points
    2021-06-19T17:05:11.887+00:00

    Since there are many free converters available to do
    this task, we may assume that you are trying to code
    it yourself because it is a course assignment. If that
    is the case then you should NOT be using code that others
    have written (although your approach may follow the same
    logic and/or algorithms).

    If it is an assignment, then you need to describe what
    the context is of the task and any constraints.

    Is the "ASCII value" a decimal number?

    Or is it a printable character?

    What are you expected to use?

    What are you expected to NOT use?

    Are you allowed to use methods/functions available
    in frameworks such as .NET?

    Are you expected to write VB code that implements
    the conversion based on a portable algorithm
    which will work when coded in any programming
    language? (In the manner of number base conversions
    that implement the logic without recourse to "canned"
    conversion routines from libraries.)

    • Wayne
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.