Hi there
I want to change a Hex number to dec number in ms access, how can i do that?
in excel, just apply =hex2dec(range), .
thank you for your hekp,
cheers
Mohsin
Hi there
I want to change a Hex number to dec number in ms access, how can i do that?
in excel, just apply =hex2dec(range), .
thank you for your hekp,
cheers
Mohsin
Re: Change Hex Number To Dec Number
In detail;
My value = 319,A03,B13,16, which is in text format. The numbers actually are
in hex value. What i need to do now is, convert the value from text to hex
then to decimal,
i'm using below expression for converting to hex, but is wrong..:)
CCDec: Val(Replace([tblclearcode]![CLEARCODE_ID],",",""))
Thank you
Mohsin
Re: Change Hex Number To Dec Number
Are you wanting a query result where you select the text field to be represented as a Hex? Is this code to store a Hex number in a table from a textbox on a form? Please explain the context of where you want the conversion to take place.
Re: Change Hex Number To Dec Number
Hi
Yes, text field to hex format..
thank you
Re: Change Hex Number To Dec Number
There has to be a better way to do this but for the interim, this works:
Function hex_2_num(hexnum As String) As Variant
Dim bogus As Boolean
bogus = False
For x = 1 To Len(hexnum)
Select Case Mid$(hexnum, x, 1)
Case Is = "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
Case Else
bogus = True
End Select
Next x
If Not bogus Then
Dim hexes(5, 1) As String
hexes(0, 0) = "A"
hexes(0, 1) = "10"
hexes(1, 0) = "B"
hexes(1, 1) = "11"
hexes(2, 0) = "C"
hexes(2, 1) = "12"
hexes(3, 0) = "D"
hexes(3, 1) = "13"
hexes(4, 0) = "E"
hexes(4, 1) = "14"
hexes(5, 0) = "F"
hexes(5, 1) = "15"
For x = Len(hexnum) To 1 Step -1
Position = Len(hexnum) - x + 1
If Position > 8 Then
MsgBox "Number too large"
'Call clearcontents
Exit Function
End If
If IsNumeric(Mid$(hexnum, x, 1)) Then
Select Case Position
Case Is = 1
Position = 1
Case Is = 2
Position = 16
Case Is = 3
Position = 256
Case Is = 4
Position = 4096
Case Is = 5
Position = 65536
Case Is = 6
Position = 1048576
Case Is = 7
Position = 16777216
Case Is = 8
Position = 268435456
End Select
returnvalue = returnvalue + (Position * CInt(Mid$(hexnum, x, 1)))
Else
For y = 0 To 5
If hexes(y, 0) = Mid$(hexnum, x, 1) Then
multiplier = hexes(y, 1)
End If
Next y
Select Case Position
Case Is = 1
Position = 1
Case Is = 2
Position = 16
Case Is = 3
Position = 256
Case Is = 4
Position = 4096
Case Is = 5
Position = 65536
Case Is = 6
Position = 1048576
Case Is = 7
Position = 16777216
Case Is = 8
Position = 268435456
End Select
returnvalue = returnvalue + (Position * CInt(multiplier))
End If
Next x
hex_2_num = returnvalue
Else
MsgBox "This is not a valid hex value"
'Call clearcontents
End If
hex_2_num = returnvalue
End Function
Display More
Re: Change Hex Number To Dec Number
I had a similar problem of needing to convert hex2dec or dec2hex. application had to determine which coversion was needed on a case by case basis. It is part of an old inventory receiving program for Cell phone. Cell phones used to have HEX & DEC Electronic Serial Numbers. Now they are using IMEI so I don't believe this bit of code what you want but may give some insite...
the HEXESN is always 8 digits
the DECESN is always 11 digits
the first 2 HEX digits equate to the first 3 digits in the DEC
Function ConvertESN(strESN As String, strHex As String, strDec As String) As Boolean
ConvertESN = False
Select Case Len(strESN)
Case Is = 11
If IsNumeric(strESN) Then
strHex = HEX(Left$(strESN, 3)) & HEX(Right$(strESN, 8))
strDec = strESN
ConvertESN = True
If Len(strHex) > 8 Then ConvertESN = False
End If
Case Is = 8
If IsValidHex(strESN) Then
strDec = GetDecimal(strESN)
strHex = strESN
ConvertESN = True
End If
End Select
End Function
Function IsValidHex(strESN As String) As Boolean
Dim intPos As Integer
Dim strValidHexValues As String
strValidHexValues = "0123456789ABCDEF"
IsValidHex = True
For intPos = 1 To Len(strESN)
If InStr(strValidHexValues, Mid$(strESN, intPos, 1)) = 0 Then
IsValidHex = False
Exit Function
End If
Next
End Function
Function GetDecimal(strESN As String) As String
Dim lngDecESN As Long
Dim strHexValues As String
strHexValues = "123456789ABCDEF"
' First two bytes
lngDecESN = 16 * InStr(1, strHexValues, Mid$(strESN, 1, 1))
lngDecESN = lngDecESN + InStr(1, strHexValues, Mid$(strESN, 2, 1))
GetDecimal = lngDecESN
' Next six bytes
lngDecESN = 1048576 * InStr(1, strHexValues, Mid$(strESN, 3, 1))
lngDecESN = lngDecESN + 65536 * InStr(1, strHexValues, Mid$(strESN, 4, 1))
lngDecESN = lngDecESN + 4096 * InStr(1, strHexValues, Mid$(strESN, 5, 1))
lngDecESN = lngDecESN + 256 * InStr(1, strHexValues, Mid$(strESN, 6, 1))
lngDecESN = lngDecESN + 16 * InStr(1, strHexValues, Mid$(strESN, 7, 1))
lngDecESN = lngDecESN + InStr(1, strHexValues, Mid$(strESN, 8, 1))
' The above code will drop a leading zero in the "next six bytes".
' this next line of code was added to
' Handle 7-byte decimals - this is the only line I added ...
If Left(Str(lngDecESN), 1) = " " Then GetDecimal = GetDecimal & "0"
GetDecimal = GetDecimal & lngDecESN
End Function
Display More
Re: Change Hex Number To Dec Number
This may help:
Function H2D(s As String) As Variant
Dim i As Long, sc As String
For i = 1 To Len(s)
sc = Mid(s, i, 1)
Select Case sc
Case "0" To "9": H2D = 16 * H2D + sc - 48
Case "A" To "F": H2D = 16 * H2D + sc - 55
Case "a" To "f": H2D = 16 * H2D + sc - 87
Case Else
H2D = "#VALUE!"
Exit Function
End Select
Next
End Function
Display More
Re: Change Hex Number To Dec Number
The code I pasted last week had a bug. This may be OBE, but here's a correction.
Function H2D(s As String) As Variant
' convert hex string to decimal
Dim i As Long, sc As String
For i = 1 To Len(s)
sc = Mid(s, i, 1)
Select Case sc
Case "0" To "9": H2D = 16 * H2D + Asc(sc) - 48
Case "A" To "F": H2D = 16 * H2D + Asc(sc) - 55
Case "a" To "f": H2D = 16 * H2D + Asc(sc) - 87
Case Else
H2D = "#VALUE!"
Exit Function
End Select
Next
End Function
Display More
Don’t have an account yet? Register yourself now and be a part of our community!