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

后端 未结 9 1792
自闭症患者
自闭症患者 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:28

    This will convert your string into an array of bytes:

    Dim hex As String = "A14152464C203230304232323020572F544947455234352E"
    
    Dim len As Integer = hex.Length \ 2
    Dim data(len - 1) As Byte
    For i As Integer = 0 to len - 1
       data(i) = Convert.ToByte(hex.Substring(i * 2, 2), 16)
    Next
    
    0 讨论(0)
  • 2020-12-11 03:29

    Write one yourself.

    You will need to tokenize the string, then start from the right, and work your way left.

    int weight = 1;
    While Looping
    {
    
      If (token(i) == "F") { DecimalValue += 15 * weight; }
      If (token(i) == "E") { DecimalValue += 14 * weight; }
      If (token(i) == "D") { DecimalValue += 13 * weight; }
      If (token(i) == "C") { DecimalValue += 12 * weight; }
      If (token(i) == "B") { DecimalValue += 11 * weight; }
      If (token(i) == "A") { DecimalValue += 10 * weight; }
      else { DecimalValue += token(i) * weight; }
    
      weight = weight * 16;
    }
    

    Something like that.

    0 讨论(0)
  • 2020-12-11 03:31

    You can use the Val function in Visual Basic to convert a hexadecimal value to a decimal value. This is done by prefixing the string with "&H", to tell Visual Basic that this is a hexadecimal value, and then convert that into a number.

    Dim Value As Integer = Val("&H" & YourHexadecimalStringHere)
    
    0 讨论(0)
提交回复
热议问题