Converting From Decimal to Custom Counting Definition
-
Hello There, Please keep in mind that I am not the brightest person in the bunch, that is why I am here asking for help. Because of my lack of education in math, I've been kind of creating my own solutions =/ This code is written in Microsoft Visual Basic for Applications (VBA) 7.0 and used in a Microsoft Access 2010 application. Sometime in the past year, I created a function that converted non-decimal based string values. Like HEX "0123456789ABCDEF" and/or any other numbering definition you can think of. I have recently come across another application that requires a custom format of numbering. Bringing me to revise the old function with the new, containing a formula that I wrote out. The Revised function: Code:
'---------------------------------------------------------------------------------------------------'
'|
'| Date Created: Feb, 27 2015
'| BY: Daniel Couillard
'| Written In: Microsoft Visual Basic for Applications (VBA) 7.0
'|
'| Revisions:
'|
'| Date Revised: Dec, 21 2015
'| BY: Daniel Couillard
'| Revision:
'| - Created Mathmatical formula to shorten code.
'| - Re-commented with respect to the changes.
'|
'| .-------------------------------------.
'| | Function - ConvertStringToDecimal |
'| `-------------------------------------`
'|
'| PARAMETERS:
'|
'| * str - Contains the value to be converted to decimal.
'|
'| * def - Contains the definition that describes the value's
'| format.
'|
'| For example, HEX's definition would be: "0123456789ABCDEF"
'|
'| RETURN:
'|
'| * variant - _
'|
'| The return value is a variant because it can contain one of the two following results:
'|
'| 1. (STRING) Error Message.
'| 2. (DECIMAL) Calculated Value.
'| We use the Decimal data type because of its abillity to store such a large number.
'|
'|
'| NOTE:
'|
'| FURTHER EXPLAINATION CAN BE FOUND WITHIN THE FUNCTION
'|
'---------------------------------------------------------------------------------------------------'Public Function ConvertStringToDecimal(ByVal str As String, _
ByVal def As String) As Variant' ------------------------------------- ' ' Prep Parameters. ' ' ------------------------------------- ' str = Trim(str & vbNullString) def = Trim(def & vbNullString) ' ------------------------------------- ' ' ---------------------
-
Hello There, Please keep in mind that I am not the brightest person in the bunch, that is why I am here asking for help. Because of my lack of education in math, I've been kind of creating my own solutions =/ This code is written in Microsoft Visual Basic for Applications (VBA) 7.0 and used in a Microsoft Access 2010 application. Sometime in the past year, I created a function that converted non-decimal based string values. Like HEX "0123456789ABCDEF" and/or any other numbering definition you can think of. I have recently come across another application that requires a custom format of numbering. Bringing me to revise the old function with the new, containing a formula that I wrote out. The Revised function: Code:
'---------------------------------------------------------------------------------------------------'
'|
'| Date Created: Feb, 27 2015
'| BY: Daniel Couillard
'| Written In: Microsoft Visual Basic for Applications (VBA) 7.0
'|
'| Revisions:
'|
'| Date Revised: Dec, 21 2015
'| BY: Daniel Couillard
'| Revision:
'| - Created Mathmatical formula to shorten code.
'| - Re-commented with respect to the changes.
'|
'| .-------------------------------------.
'| | Function - ConvertStringToDecimal |
'| `-------------------------------------`
'|
'| PARAMETERS:
'|
'| * str - Contains the value to be converted to decimal.
'|
'| * def - Contains the definition that describes the value's
'| format.
'|
'| For example, HEX's definition would be: "0123456789ABCDEF"
'|
'| RETURN:
'|
'| * variant - _
'|
'| The return value is a variant because it can contain one of the two following results:
'|
'| 1. (STRING) Error Message.
'| 2. (DECIMAL) Calculated Value.
'| We use the Decimal data type because of its abillity to store such a large number.
'|
'|
'| NOTE:
'|
'| FURTHER EXPLAINATION CAN BE FOUND WITHIN THE FUNCTION
'|
'---------------------------------------------------------------------------------------------------'Public Function ConvertStringToDecimal(ByVal str As String, _
ByVal def As String) As Variant' ------------------------------------- ' ' Prep Parameters. ' ' ------------------------------------- ' str = Trim(str & vbNullString) def = Trim(def & vbNullString) ' ------------------------------------- ' ' ---------------------
Here is a simplified version of the first function.
Public Function ConvertStringToDecimal(ByVal str As String, _
ByVal def As String) As Variant' ------------------------------------- ' ' Parse Parameters. ' ' ------------------------------------- ' If Len(str) = 0 Then: ConvertStringToDecimal = "No value has been entered.": Exit Function If Len(def) < 2 Then: ConvertStringToDecimal = "Number definition must have 2 or more characters.": Exit Function ' ------------------------------------- ' ' ------------------------------------- ' ' Variable Declaration & Initialization ' ' ------------------------------------- ' Dim Base As Integer: Base = Len(def) ' - Length of Definition is the Base Dim LV As Integer: LV = Len(str) ' - Length of Value Dim N As Variant: N = CDec(0) ' - The sum of the calculations. Dim I As Integer ' - Increment. Dim P As Integer ' - Position of Value\[i\] in Reverse Definition. ' ------------------------------------- ' ' Traverse through the characters(digits) of the string in ' an incremental order. For I = 1 To LV P = InStr(1, def, Mid(str, I, 1)) if P = 0 then ConvertStringToDecimal = "Unknown digit.": Exit Function N= N \* Base + P - 1 Next ' Return the calculated value. ConvertStringToDecimal = N
End Function
you should not prep the parameters because it prevent the use of space in definition and removing spaces in definition is changing the values of digits.
Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein
-
Here is a simplified version of the first function.
Public Function ConvertStringToDecimal(ByVal str As String, _
ByVal def As String) As Variant' ------------------------------------- ' ' Parse Parameters. ' ' ------------------------------------- ' If Len(str) = 0 Then: ConvertStringToDecimal = "No value has been entered.": Exit Function If Len(def) < 2 Then: ConvertStringToDecimal = "Number definition must have 2 or more characters.": Exit Function ' ------------------------------------- ' ' ------------------------------------- ' ' Variable Declaration & Initialization ' ' ------------------------------------- ' Dim Base As Integer: Base = Len(def) ' - Length of Definition is the Base Dim LV As Integer: LV = Len(str) ' - Length of Value Dim N As Variant: N = CDec(0) ' - The sum of the calculations. Dim I As Integer ' - Increment. Dim P As Integer ' - Position of Value\[i\] in Reverse Definition. ' ------------------------------------- ' ' Traverse through the characters(digits) of the string in ' an incremental order. For I = 1 To LV P = InStr(1, def, Mid(str, I, 1)) if P = 0 then ConvertStringToDecimal = "Unknown digit.": Exit Function N= N \* Base + P - 1 Next ' Return the calculated value. ConvertStringToDecimal = N
End Function
you should not prep the parameters because it prevent the use of space in definition and removing spaces in definition is changing the values of digits.
Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein
P will always end up being 0 at some point. Also, great suggestion for not trimming at the beginning. That was a last minute add that I put in there. Thank you for your suggestions with the first function.
-
P will always end up being 0 at some point. Also, great suggestion for not trimming at the beginning. That was a last minute add that I put in there. Thank you for your suggestions with the first function.
-
BlueIshDan wrote:
P will always end up being 0 at some point.
Only if you provide an unknown digit.
Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein
Crap I tried to fix my message, I read the code too fast to notice that you changed a bit more than I'd first thought. Thank you again for your suggestions on the first function.
-
BlueIshDan wrote:
P will always end up being 0 at some point.
Only if you provide an unknown digit.
Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein
I'm blown away by N= N * Base + P - 1. Its going to take me a bit of figuring out to learn this! :)
-
I'm blown away by N= N * Base + P - 1. Its going to take me a bit of figuring out to learn this! :)
-
Hello There, Please keep in mind that I am not the brightest person in the bunch, that is why I am here asking for help. Because of my lack of education in math, I've been kind of creating my own solutions =/ This code is written in Microsoft Visual Basic for Applications (VBA) 7.0 and used in a Microsoft Access 2010 application. Sometime in the past year, I created a function that converted non-decimal based string values. Like HEX "0123456789ABCDEF" and/or any other numbering definition you can think of. I have recently come across another application that requires a custom format of numbering. Bringing me to revise the old function with the new, containing a formula that I wrote out. The Revised function: Code:
'---------------------------------------------------------------------------------------------------'
'|
'| Date Created: Feb, 27 2015
'| BY: Daniel Couillard
'| Written In: Microsoft Visual Basic for Applications (VBA) 7.0
'|
'| Revisions:
'|
'| Date Revised: Dec, 21 2015
'| BY: Daniel Couillard
'| Revision:
'| - Created Mathmatical formula to shorten code.
'| - Re-commented with respect to the changes.
'|
'| .-------------------------------------.
'| | Function - ConvertStringToDecimal |
'| `-------------------------------------`
'|
'| PARAMETERS:
'|
'| * str - Contains the value to be converted to decimal.
'|
'| * def - Contains the definition that describes the value's
'| format.
'|
'| For example, HEX's definition would be: "0123456789ABCDEF"
'|
'| RETURN:
'|
'| * variant - _
'|
'| The return value is a variant because it can contain one of the two following results:
'|
'| 1. (STRING) Error Message.
'| 2. (DECIMAL) Calculated Value.
'| We use the Decimal data type because of its abillity to store such a large number.
'|
'|
'| NOTE:
'|
'| FURTHER EXPLAINATION CAN BE FOUND WITHIN THE FUNCTION
'|
'---------------------------------------------------------------------------------------------------'Public Function ConvertStringToDecimal(ByVal str As String, _
ByVal def As String) As Variant' ------------------------------------- ' ' Prep Parameters. ' ' ------------------------------------- ' str = Trim(str & vbNullString) def = Trim(def & vbNullString) ' ------------------------------------- ' ' ---------------------
And the simplified second function:
Public Function ConvertNumberToDefinition(ByVal number As Variant, _
ByVal def As String) As StringDim Base As Integer: Base = Len(def) Dim val As String: val = "" Dim temp As Variant While number > 0 temp = number Mod base val= Mid(def, temp + 1, 1) & val number= ( number - temp ) / base End While ConvertNumberToDefinition = val
End Function
Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein
-
And the simplified second function:
Public Function ConvertNumberToDefinition(ByVal number As Variant, _
ByVal def As String) As StringDim Base As Integer: Base = Len(def) Dim val As String: val = "" Dim temp As Variant While number > 0 temp = number Mod base val= Mid(def, temp + 1, 1) & val number= ( number - temp ) / base End While ConvertNumberToDefinition = val
End Function
Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein
Patrice, Thank you very much for your guidance! Rest can be assured that I am studying from these and learning. Kindest Regards Daniel :)
-
Patrice, Thank you very much for your guidance! Rest can be assured that I am studying from these and learning. Kindest Regards Daniel :)