How do I convert Hex to an Integer?
Printable View
How do I convert Hex to an Integer?
val("&HFF") = 255
so val("&h" & hex_val)
You missed the point DigitalError. The thing i'm trying to find is:
VB Code:
Function ConvertHex(Input as String) as Integer Output = Something(Input) ConvertHex = Output End Function
If you are sure that conversion will remain within Integer bounds, then use the CInt function, otherwise use the CLng function.
Those functions do not work with STRING type Hex.
The hex I am using is not the "&H??" its more like a string type.
eg.
VB Code:
i = ConvertHex("FF") 'i = 255
Why don't you append the &H at the begining?
VB Code:
Private Function ConvertHex(ByRef HexValue As String) As Long If Not UCase(Left$(HexValue, 2)) = "H&" Then HexValue = "&H" & HexValue ConvertHex = CLng(HexValue) End Function
Because wrighting filenames.
eg.
Filename.002
Filename.0BF
VB Code:
'ID = 0 to 4096 iHex = ConvertToHex(ID) OpenAsTextStream(Filename & iHex, blah, blah) 'iHex = number after conversion from i 'Then: ID=ConvertToI(Right(Filename, 3)) '^^^ this is what i mean.
Long Story Short. Dont ask why the hell I'm doing this, because it is too hard & long to explain.
Here this will help:
VB Code:
Filename = User_ID.1AF 'Sample File ID = ConvertToInteger(Right(Filename, 3) 'Converts the"1AF" to an Integer. ID = ?
VB Code:
ID = ConvertToInteger("&H" & Right(Filename, 3)
And then In ConvertToInteger function use CInt.
I'll post the rest of what the hell I'm doing tomarrow. But that works thanx. :)
You add the &H in the conversion function, from the's original value.
Duh.
here:
VB Code:
Function HexToInt(byval HexDat as string) as Integer HexIoInt = val("&h" & HexDat) End Function
To use:
i HexToInt("FF")