How to convert a string to hex in VB.NET?

How to convert a string from a text field to hexadecimal?

I just found ways in C #. Does VB.NET have the ability to do such things? If possible, I would like to know how to convert a string in hex and hex to a string.

+3
source share
7 answers
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".

+9
source
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
+3
source

:

Convert.ToInt32(15, 16)

, :

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

, Convert.ToInt32(val, 16). Convert.ToInt32 , 2, 8, 10 16.

:

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

. # VB .

+2

, .

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

:

Debug.WriteLine(StringToHex("sim0n"))

+1

String s LINQ:

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

:

► fix 25BA 0020 0066 0069 0078.

!

, , Unicode, , Un-Unicode ASCII , .

0

, , .
value/targetBase .
Fix(), , .
, .

, , IntToString.

:

:
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
:
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
.

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

, .

I have over 25 years of 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 help this community.
I am sad that I can not comment on the answers. I hope someone will add a few points to me so that I can now help others more easily.
Because of this, I add my comment to Tilak's answer, dated May 8, 2012, as an answer. This is the first and, I hope, the only time I resort to this. Sorry for that, but I don't know another way.

0
source

All Articles