Hello Guys ,
Is there any function or method to convert a hex value to DEC ? i searched so far in the framework and i did not find to many details.
Thanks,
Aurelian
Hello Guys ,
Is there any function or method to convert a hex value to DEC ? i searched so far in the framework and i did not find to many details.
Thanks,
Aurelian
Read it into an Integer value (you probably want unsigned) with Text.FromHex, then output from there.
Use the Val
method with the &h
literal:
Dim hexnumber As String = "FF"
Dim dec As Integer = Val("&h" + hexnumber)
Thanks, i found another way and it seems to work .
dim data as string
dim num as Integer
data = hex value as string
num=Val("&h"+data).ToText
Thanks again.
[quote=322824:@Andrew Lambert]Use the Val
method with the &h
literal:
Dim hexnumber As String = "FF"
Dim dec As Integer = Val("&h" + hexnumber)
[/quote]
Thanks , i did same thing.
Well it seems that something is not right here , i get the following hex “0000015B06F861E8” if i use an online convertor i get this “1490470593000” but on the code that i put above i get this “116941288” so what i`m doing wrong and where is the issue ?
Thanks
Maybe the 00’s as prefix arn’t what you need?
They’re irrelevant in any case
Like writing 10 as 00010 - its still 10 regardless of the number of leading 0’s
It looks like Val is only taking the lower 32 bits (116941288 = &h0000000006F861E8). This should work (without &h
):
Function ConvertHex64(Value As String) As UInt64
Dim symbol() As String = Split("0123456789ABCDEF", "")
Dim result As UInt64
For i As Integer = 1 To Value.Len
Dim digit As String = Value.Mid(i, 1)
Const base = 16
result = base * result + symbol.IndexOf(digit)
Next
Return result
End Function
I’d skip the split & lookup by index since instr will give you the same result used creatively
and it seems to make this function run quite a bit quicker in a quick test (100,000 iterations of the conversion for yours & the modified one and the original took 1760178 microseconds the modified 935611 - just about 2x faster)
Function ConvertHex64(Value As String) As UInt64
Dim result As UInt64
For i As Integer = 1 To Value.Len
Dim digit As integer = instr("0123456789ABCDEF", value.mid(i,1)) - 1
if digit < 0 then
break
// should probably bail out herewith an exception as this is no longer a hex string at this point
end if
Const base = 16
result = base * result + digit
Next
Return result
end function
[quote=322865:@Norman Palardy]I’d skip the split & lookup by index since instr will give you the same result used creatively
and it seems to make this function run quite a bit quicker in a quick test (100,000 iterations of the conversion for yours & the modified one and the original took 1760178 microseconds the modified 935611 - just about 2x faster)
Function ConvertHex64(Value As String) As UInt64
Dim result As UInt64
For i As Integer = 1 To Value.Len
Dim digit As integer = instr("0123456789ABCDEF", value.mid(i,1)) - 1
if digit < 0 then
break
// should probably bail out herewith an exception as this is no longer a hex string at this point
end if
Const base = 16
result = base * result + digit
Next
Return result
end function
[/quote]
Thanks Norman, it did the job.
This also seems to work:
Dim hexValue As Text = "0000015B06F861E8"
Dim decValue As Int64 = Int64.FromHex(hexValue)
' decValue = 1490470593000