converting hex value to integer

Kurt

Regular
Joined
Feb 14, 2003
Messages
99
Location
Copenhagen
Is there a more nice/elegant way of converting a string representing a hexadecimal value to an integer value? At the moment I am doing something like...

Visual Basic:
Public Function MakeInt1(ByVal HexValue As String) As Integer
    Return CType("&H" & HexValue, Integer)
End Function

Amazingly this works, while the folowing doesn't

Visual Basic:
Public Function MakeInt2(ByVal HexValue As String) As Integer
    Return Convert.ToInt32("&H" & HexValue)
End Function

any comments are more then welcome...
 
Yes, it also seems to take a string as argument.
Visual Basic:
private const WS_EX_TRANSPARENT as integer = cint("&H20")
I assume Cint(x) and Ctype(x,integer) are equal. The Convert - class must work differently...
 
aahhh!! I don't understand :confused: .

I tried doing this on Visual Basic...You see what I want is this:

Someone types a number into TextBox1 (Hex), and then clicks Button1, and shows the Integer in TextBox2 (Integer).

And how could I do vice-versa? Integer --> Hex?
 
dynamic_sysop said:
you can do this also , much simpler....
Visual Basic:
Dim x As Integer = Integer.Parse(&H20&)
MessageBox.Show(x)

Cool!!

What does &H20& do?

And where could I find the codes like &H20&?

And was does Parse do?
 
&H20& is hex for 32. Integer.Parse takes a string value and converts it to an integer. I'm not so sure that dynamic_sysop's example is exactly what Kurt wants, since &H20& is recognized by VB as a number regardless of being parsed.

You need to use Convert.ToInt32() and specify the base.
Visual Basic:
MessageBox.Show(Convert.ToInt32("20", 16))
Will show 32. Note you shouldn't include the &H part.
 
Back
Top