Convert hex string (char []) to int?

前端 未结 13 923
栀梦
栀梦 2020-11-22 12:12

I have a char[] that contains a value such as \"0x1800785\" but the function I want to give the value to requires an int, how can I convert this to an int? I have searched a

13条回答
  •  北海茫月
    2020-11-22 12:55

    I know this is really old but I think the solutions looked too complicated. Try this in VB:

    Public Function HexToInt(sHEX as String) as long
    Dim iLen as Integer
    Dim i as Integer 
    Dim SumValue as Long 
    Dim iVal as long
    Dim AscVal as long
    
        iLen = Len(sHEX)
        For i = 1 to Len(sHEX)
          AscVal = Asc(UCase(Mid$(sHEX, i,  1)))
          If AscVal >= 48 And AscVal  <= 57 Then
            iVal  = AscVal - 48
          ElseIf AscVal >= 65 And AscVal <= 70 Then
            iVal = AscVal - 55
          End If 
          SumValue = SumValue + iVal * 16 ^ (iLen- i)
        Next i 
        HexToInt  = SumValue
    End Function 
    

提交回复
热议问题