(fun) dynamic number to decimal converter - spot errors please :d (1 Viewer)

BlueIshDan

☠
Local time
Today, 18:50
Joined
May 15, 2014
Messages
1,122
I've been looking into writing an access form used for hard drive analysis and came across hex values. This made me start dabbling with numbers again and came up with the following function.

If you're a person that enjoys playing with numbers, could I get you to think of any scenarios where I could solidify this function to handle errors and become as dynamic as possible!

I think the end result could be useful to add to this communities quick fix library. :)

What it does is take two strings, value and and the definition of how that numbers counts, and with these two values it will spit out the decimal value.

examples:
HEX - "FFF", "0123456789ABCDEF"
OCT - "777", "01234567"
BINARY - "11111110", "01"
Code:
Public Function ConvertStringToDecimal(ByVal str As String, _
                                       ByVal def As String) As Variant
                                       
    Dim inc As Integer: inc = Len(def)
    Dim n As Variant: n = CDec(0)
    Dim i As Integer
    Dim val As Integer
    
    For i = Len(str) To 1 Step -1
        val = InStr(1, def, Mid(str, i, 1))
        If val > 0 Then: n = n + CDec((val - 1) * (inc ^ (i - 1)))
    Next
    
    ConvertStringToDecimal = n
    
End Function

Thanks,
Blue
 
Last edited:

BlueIshDan

☠
Local time
Today, 18:50
Joined
May 15, 2014
Messages
1,122
I want you to know this isn't a "HEY LOOK WHAT I CAN DO" post. I genuinely love to code and learn about things like this.
 

BlueIshDan

☠
Local time
Today, 18:50
Joined
May 15, 2014
Messages
1,122
Commented Version for learners =]

Code:
Public Function ConvertStringToDecimal(ByVal str As String, _
                                       ByVal def As String) As Variant
                                       
    ' Parse Parameters. '
    If Len(Trim(str)) = 0 Then: ConvertStringToDecimal = "No value has been entered.": Exit Function
    If Len(Trim(def)) < 2 Then: ConvertStringToDecimal = "Number definition must have 2 or more characters.": Exit Function
    ' ----------------- '
    
                                       
    ' Variable Declaration & Initialization '
    Dim inc As Integer: inc = Len(def)      ' Used to define the value of each position. inc ^ (pos - 1)
    Dim n As Variant: n = CDec(0)           ' Used to store the sum of the calculations.
    Dim i As Integer                        ' Used to define the loop's variable.
    Dim val As Integer                      ' Used to store calculated values. See more below.
    ' ------------------------------------- '
    
    ' Reverse string for correct digit position calculation   '
    ' Allows for minimization in calculations when traversing '
    ' through string position & Or simplifies visualiztion of '
    ' the code for the reader.                                '
    str = ReverseString(str)
    
    ' Traverse through the characters(digits) of the string in '
    ' an incremental order.                                    '
    For i = 1 To Len(str)
    
        ' Store the calculated the value into a variable for readability   '
        ' Used in condition statement and in the calculated value being '
        ' added to the result 'n(sum)'.                                       '
        val = InStr(1, def, Mid(str, i, 1))
        
        ' If the value is greater than '>' 0,
        '   calculate the value of the number in its position. This is
        '   value in respect to the length of the definition. Decimal's
        '   incremental value is 10. With (val * (inc ^ (pos - 1)), the value
        '   of 1 in the 3rd position would be equal to (1 * (10 ^ 2).
        '
        '   With a more irregular number definition, like Hex, the same rules
        '   apply. Hex has a definition of "0123456789ABCDEF". The inc of this
        '   definition is 16 'length of definition'. Getting the Decimal value of
        '   'FF' uses the same function. (val * (inc ^ (pos - 1), BUT we have to now
        '   check where the the true value of 'val'. Val can be defined by the
        '   position of the character being evaluated. 'F' the first character in the
        '   number being parse is the 16th character in the number definition.
        '
        '   That makes val equal to the position of the character in the defifinition,
        '   minus 1. You're taking away one because no value is always the first value
        '   in any definition.
        '
        '   val = defPosition - 1
        '
        '   This doesnt change the function, it just looks deeper into it.
        '
        '   ( defPosition - 1 ) * (inc ^ (pos - 1)
        '
        '   You could look even deeper by writing definitive expressions for each of the
        '   variables seen in that function.
        '
        '   ( defPosition - 1) * ( ( lengthOfDef ) ^ ( (position in ( reversed order value ) ) - 1 )
        '
        
        If val > 0 Then: n = n + CDec((val - 1) * (inc ^ (i - 1)))
        
    Next
    
    ' Return the calculated value.
    ConvertStringToDecimal = n
    
End Function
 

CJ_London

Super Moderator
Staff member
Local time
Today, 22:50
Joined
Feb 19, 2013
Messages
16,610
I want you to know this isn't a "HEY LOOK WHAT I CAN DO" post.
@Blue - we're not ignoring you, just in awe:D
 

Users who are viewing this thread

Top Bottom