none
String to Hexadecimal

    Question

  • Hi folks!

    I need to be able to convert a string, including both text and numbers, to a hexadecimal value.

    Currently, I can convert numbers to hexadecimal, but it will not allow for text in any way:

    Dim MyHex As String
    MyHex = Hex(txtPassword.Text)
    MessageBox.Show(MyHex)

    Any ideas greatly appreciated!
    Tim

    Friday, January 13, 2006 5:49 PM

Answers

  • Hi Tim,

    Is this what you want? It returns the hex representation of whatever string supplied.

    For example it will return "7A" if you supply it "z".

    Imports System.Text

        Private Function getHex(ByVal str As String) As String
            Dim enc As New ASCIIEncoding
            Dim output As New StringBuilder

            For Each b As Byte In enc.GetBytes(str)
                output.Append(Hex(b))
            Next

            Return output.ToString
        End Function

    Saturday, January 14, 2006 9:40 PM

All replies

  • Private Sub txtNumber_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumber.KeyPress

    If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then

    Try

    Select Case True

    Case Is = Me.optOct.Checked

    CurrentNum = Val("&O" & LTrim(txtNumber.Text) & "&")

    Case Is = Me.optDec.Checked

    CurrentNum = Val(LTrim(txtNumber.Text) & "&")

    Case Is = Me.optHex.Checked

    CurrentNum = Val("&H" & LTrim(txtNumber.Text) & "&")

    Case Is = Me.optBin.Checked

    CurrentNum = Bin(Val(txtNumber.Text))

    End Select

    Catch ex As Exception

    MessageBox.Show(ex.Message)

    End Try

    End If

    End Sub

     

    Above code taken from one of my conversion tools that will convert hex, decimal, oct, and Binary...

     

    Friday, January 13, 2006 6:05 PM
  • Ok, now I'm confused!!

    I'm sorry if this sounds stupid, but I am pretty new to this.
    Would you mind explaining how this works?

    Tim

    Friday, January 13, 2006 7:04 PM
  • What he is doing in that post is converting a string containing a octal, hex, or decimal string representation of a number to its Integer representation.  I'm not quite sure what the binary case is doing.

    What you seem to be trying to do is convert a string to a number and then get the hexadecimal string representation of that number.  I'm not sure what you mean by using a string instead of just numbers.  If you could explain that better, I should be able to help you.

    If you want to convert the text of a textbox to number and get the string representation then the code would look like this

    Dim number As Integer = Integer.Parse(textbox1.Text)
    Dim hexString As String = number.ToString("X")
    'hexString now has the hexidecimal string representation
    'of number (which is the value in textbox1)


    This is assuming that the number is input in decimal format. if it is in binary or octal, you will need to do custom parsing.
    Friday, January 13, 2006 8:41 PM
  • Ok. Thanks for your reply.

    What I need is some code that will convert a string, that contains both text and numbers, into hexidecimal format.

    Everything I have tried so far only converts numbers into hexidecimal and not text.

    I have tried your code from the previous post, and this works correctly, but only converts numbers into hexidecimal.

    Any ideas?

    Tim

    Friday, January 13, 2006 10:40 PM
  • Tim, give us a sample of what you need to convert... it still doesn't seem clear.

    Converting a number like "3252" to Hex is clear to us.... converting a combination seems to mean something like this "32a35" ... well, that is Hex already... little bit more detail please :)
    Saturday, January 14, 2006 5:35 AM
  • Sorry! I havnt used this forum much before!

    I would like to convert a string of both text and numbers (not already in hex format) to hexidecimal format.

    The string that is to be converted is a password that the user has typed into a text field on the form e.g. txtPassword.Text
    Because I dont know what the users password is, it could be anything, it could be numbers, it could be text or it could be a random combination of both e.g. 7redcars.

    I hope this is a bit clearer!

    Tim

    Saturday, January 14, 2006 8:18 PM
  • Hi Tim,

    Is this what you want? It returns the hex representation of whatever string supplied.

    For example it will return "7A" if you supply it "z".

    Imports System.Text

        Private Function getHex(ByVal str As String) As String
            Dim enc As New ASCIIEncoding
            Dim output As New StringBuilder

            For Each b As Byte In enc.GetBytes(str)
                output.Append(Hex(b))
            Next

            Return output.ToString
        End Function

    Saturday, January 14, 2006 9:40 PM
  • Fantastic! Cheers m8!

    Tim

    Sunday, January 15, 2006 12:13 PM
  • Any change anybody knows how to convert a string, as previously, to REG_BINARY format?

    Also have a similar problem with converting a string to REG_DWORD.

    Any ideas?

    Tim

    Sunday, January 15, 2006 12:23 PM