Mark Chai
Mark Chai

Reputation: 203

How do you convert a string into hexadecimal in VB.NET?

How do I convert a string from a textbox into hexadecimal?

I have only found ways in C#. Does VB.NET have the ability to do such a thing? If it can then I'd like to know how to convert string to hex and hex to string.

Upvotes: 5

Views: 83260

Answers (7)

Evert
Evert

Reputation: 21

In my humble opinion the code of Tilak seems OK, but is not.
The rounding of value / targetBase gives results that are too large.
By using Fix() the resulting integers will be as they should be.
I compliment Tilak for finding such a neat solution for the question.

In the accompanying code I will show how functions like IntToString can be tested easily.

The expected message in the message box is:

The old results are;
0 1 10 101 100 101 1010 1001 1000 1001 1010
0 1 1X 10 11 1XX 1X0 1X1 10X 100 101
0 1 X 1y 10 11 XX Xy X0 X1 XX
0 1 X 1y 1z 10 11 1X Xy Xz X0
The new results are;
0 1 10 11 100 101 110 111 1000 1001 1010
0 1 X 10 11 1X X0 X1 XX 100 101
0 1 X y 10 11 1X 1y X0 X1 XX
0 1 X y z 10 11 1X 1y 1z X0
The new results are better IMHO.

Public Sub test()
    Dim numberCharacters As String = "01Xyz"
    Dim messageText As String = "The old results are;" & vbNewLine
    For loopCount As Integer = 1 To 2
        If loopCount = 2 Then messageText &= "The new results are;" & vbNewLine
        For baseLength As Integer = 2 To 5
            Dim baseCharacters As Char() = _
                Strings.Left(numberCharacters, baseLength).ToArray
            For integerValue As Integer = 0 To 10
                Dim resultText As String = _
                    Me.IntToString(integerValue, baseCharacters, loopCount = 2)
                messageText &= resultText & " "
            Next
            messageText &= vbNewLine
        Next
    Next
    Call MsgBox(berichtTekst & "The new results are better IMHO.")
End Sub

Public Function IntToString(value As Integer, baseChars As Char(), _
        Optional newCode As Boolean = False) As String
    Dim result As String = String.Empty
    Dim targetBase As Integer = baseChars.Length
    Do
        result = baseChars(value Mod targetBase) & result
        If newCode Then ' Improved code
            value = Fix(value / targetBase)
        Else ' Original code
            value = value / targetBase
        End If
    Loop While value > 0
    Return result
End Function

Hopefully this will help others.

I have more than 25 years experience as a programmer, mainly in RPG, Cobol, Synon, CL and Basic. I also know some Delphi, Pascal and C#. I am sure I can be a help to this community.
It makes me sad that I cannot comment to answers. Hopefully someone will add me some points, so that I can more easily help others from now on.
Because of this I add my comment to the answer of Tilak, dated may 8, 2012, as an answer. This is the first and hopefully the only time that I resort to this. Sorry for that, but I know no other way.

Upvotes: 0

Cary Bondoc
Cary Bondoc

Reputation: 2988

Tried and tested code

Create this function by copy pasting it.

Function StringToHex(ByVal text As String) As String
    Dim hex As String
    For i As Integer = 0 To text.Length - 1
        hex &= Asc(text.Substring(i, 1)).ToString("x").ToUpper
    Next
    Return hex
End Function

Use it like this

Debug.WriteLine(StringToHex("sim0n"))

Source

Upvotes: 3

Habib
Habib

Reputation: 223257

Dim val As String
val = "10"
Dim hexVal As Integer
hexVal = Convert.ToInt32(val, 16) //16 specifies the base
Console.WriteLine(hexVal)

This will display 16 which is the integer equivalent of the hexadecimal string "10".

Upvotes: 11

Rasmus Søborg
Rasmus Søborg

Reputation: 3695

You can convert an integer to a hexdecimal number easily by doing:

Convert.ToInt32(15, 16)

And to convert it back to an integer, you can do:

Integer.Parse("15f", System.Globalization.NumberStyles.HexNumber)

Upvotes: 4

Tilak
Tilak

Reputation: 30698

To convert into hexadecimal, use Convert.ToInt32(val, 16). Convert.ToInt32 supports limited bases, 2, 8, 10, and 16.

To convert into any base, use:

Public Shared Function IntToString(value As Integer, baseChars As Char()) As String
    Dim result As String = String.Empty
    Dim targetBase As Integer = baseChars.Length

    Do
        result = baseChars(value Mod targetBase) + result
        value = value / targetBase
    Loop While value > 0

    Return result
End Function

The above function comes from this question. The C# to VB conversion was done using this.

Upvotes: 2

miroxlav
miroxlav

Reputation: 12194

Short and effective expression to display all characters of String s in hexadecimal form can be written using LINQ:

String.Join(" ", s.Select(Function(c) Conversion.Hex(AscW(c)).PadLeft(4, "0")).ToArray()))

Example:

For string ► fix it gives string 25BA 0020 0066 0069 0078.

Enjoy!

Please keep in mind this is Unicode-enabled, returning 4-digit hexadecimal value for every character, because old plain Non-Unicode ASCII is dead and you should no longer rely on it in any application.

Upvotes: 1

Phil
Phil

Reputation: 91

Public Function StrToHex(ByRef Data As String) As String
    Dim sVal As String
    Dim sHex As String = ""
    While Data.Length > 0
        sVal = Conversion.Hex(Strings.Asc(Data.Substring(0, 1).ToString()))
        Data = Data.Substring(1, Data.Length - 1)
        sHex = sHex & sVal
    End While
    Return sHex
End Function

Upvotes: 3

Related Questions