VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Convert Hexadecimal-Binnary-Octal Number To Decimal and reserve function.

by Vural Dincer (1 Submission)
Category: Math/Dates
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Fri 20th July 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Convert Hexadecimal-Binnary-Octal Number To Decimal and reserve function.

API Declarations


' Convert Hex-Bin-Oct To Dec And Dec To Hex-Bin-Oct Module
' written By Vural Dincer
' mail: [email protected]
' Create New Module and insert this code
Option Explicit
Option Base 1
Private division, residual As Integer
Private d1 As String
Private D2 As Variant
Public strTemp As String
Public status As Boolean
Public strLength, total As Integer
Public Const strArray = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F"
Public i, j, k As Integer
Private divisor As Integer
###########
'################### Convert Decimal2(Octal or Binary Or Hexadecimal) ####
Public Function DecToX(ByVal number As Long, div As Integer) As String
On Error GoTo errHandle
Let divisor = div
If number >= divisor Then
division = number \ divisor
residual = number - division * divisor
strTemp = strTemp & Trim(returnVal(residual, 1))
div = divisor
DecToX division, div
Else
If division = 0 Then division = number
strTemp = strTemp & returnVal(division, 1)
strLength = Len(strTemp)
For i = strLength To 1 Step -1
d1 = d1 & Mid(strTemp, i, 1)
Next i
strTemp = d1
End If
DecToX = strTemp
Exit Function
errHandle:
Debug.Print Error(Err.number)
End Function
'############# Convert (Bin or Oct or Hex)2Decimal ######################
Public Function XToDec(ByVal strCode As String, ByVal div As Integer) As Double
On Error GoTo errHandle
Let divisor = div
strLength = Len(strCode)
For i = 1 To strLength
total = total + (Val(returnVal(Mid(strCode, i, 1), 2)) * pow(divisor, strLength - i))
Next i
XToDec = total
Exit Function
errHandle:
Debug.Print Error(Err.number)
End Function
##########
Private Function pow(number As Integer, exponent As Integer) As Long
pow = number ^ exponent
End Function
##########

##########
Private Function returnVal(ByVal fnumber As Variant, ByVal flag As Integer) As Variant
If flag = 1 Then
Select Case fnumber
Case 10
D2 = "A"
Case 11
D2 = "B"
Case 12
D2 = "C"
Case 13
D2 = "D"
Case 14
D2 = "E"
Case 15
D2 = "F"
Case Else
D2 = fnumber
End Select
ElseIf flag = 2 Then
Select Case fnumber
Case "A"
D2 = 10
Case "B"
D2 = 11
Case "C"
D2 = 12
Case "D"
D2 = 13
Case "E"
D2 = 14
Case "F"
D2 = fnumber
Case Else
D2 = fnumber
End Select
End If
returnVal = D2
End Function

Public Sub setVariable()
total = 0
strTemp = ""
d1 = ""
division = 0
End Sub


Rate Convert Hexadecimal-Binnary-Octal Number To Decimal and reserve function.



Dim my_number1 As String,my_number2 As Variant

my_number1=DecToX(12,2) ' return my_number1=1100
my_number2=XToDec("AEED",16) ' return my_number2=44781
' hex from num.16 , ocx from num.8 , bin from num.2 or dec from num. 10


Download this snippet    Add to My Saved Code

Convert Hexadecimal-Binnary-Octal Number To Decimal and reserve function. Comments

No comments have been posted about Convert Hexadecimal-Binnary-Octal Number To Decimal and reserve function.. Why not be the first to post a comment about Convert Hexadecimal-Binnary-Octal Number To Decimal and reserve function..

Post your comment

Subject:
Message:
0/1000 characters