How do you convert hex to decimal using VB.NET?

后端 未结 9 1818
自闭症患者
自闭症患者 2020-12-11 03:04

I need to convert hex to a decimal in VB.NET. Found several examples in C#, but when I tried to convert to VB.NET I was not successful. An example of a hexadecimal number th

9条回答
  •  青春惊慌失措
    2020-12-11 03:10

    Private Function toByte(ByVal Shex As String) As List(Of Byte)
        Const cvtCH As Integer = 2
        Dim retval As New List(Of Byte)
        Dim rmndr As Integer
        rmndr = Shex.Length Mod cvtCH
        If rmndr <> 0 Then Shex = Shex.PadLeft(Shex.Length + cvtCH - rmndr, "0"c)
        For x As Integer = 0 To Shex.Length - 1 Step cvtCH
            retval.Add(Convert.ToByte(Shex.Substring(x, cvtCH), 16))
        Next
        Return retval
    End Function
    Private Function toU32(ByVal Shex As String) As List(Of UInt32)
        Const cvtCH As Integer = 8
        Dim retval As New List(Of UInt32)
        Dim rmndr As Integer
        rmndr = Shex.Length Mod cvtCH
        If rmndr <> 0 Then Shex = Shex.PadLeft(Shex.Length + cvtCH - rmndr, "0"c)
        For x As Integer = 0 To Shex.Length - 1 Step cvtCH
            retval.Add(Convert.ToUInt32(Shex.Substring(x, cvtCH), 16))
        Next
        Return retval
    End Function
    Private Function toU64(ByVal Shex As String) As List(Of UInt64)
        Const cvtCH As Integer = 16
        Dim retval As New List(Of UInt64)
        Dim rmndr As Integer
        rmndr = Shex.Length Mod cvtCH
        If rmndr <> 0 Then Shex = Shex.PadLeft(Shex.Length + cvtCH - rmndr, "0"c)
        For x As Integer = 0 To Shex.Length - 1 Step cvtCH
            retval.Add(Convert.ToUInt64(Shex.Substring(x, cvtCH), 16))
        Next
        Return retval
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'unsigned 32 bit max = FFFFFFFF
        'unsigned 64 bit max = FFFFFFFF
        'signed 32 bit max = 7FFFFFFF
        'signed 64 bit max = 7FFFFFFF
        Dim hexS As String = "A14152464C203230304232323020572F544947455234352E"
        Dim hexS2 As String = "A14152464C203230304232323020572F54494745523435"
        toByte(hexS)
        toU32(hexS)
        toU64(hexS)
    End Sub
    

提交回复
热议问题