Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Converting From Decimal to Custom Counting Definition

Converting From Decimal to Custom Counting Definition

Scheduled Pinned Locked Moved Algorithms
helptutoriallearning
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BlueIshDan
    wrote on last edited by
    #1

    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)
    ' ------------------------------------- '
    
    ' ---------------------
    
    P 2 Replies Last reply
    0
    • B BlueIshDan

      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)
      ' ------------------------------------- '
      
      ' ---------------------
      
      P Offline
      P Offline
      Patrice T
      wrote on last edited by
      #2

      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

      B 1 Reply Last reply
      0
      • P Patrice T

        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

        B Offline
        B Offline
        BlueIshDan
        wrote on last edited by
        #3

        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 1 Reply Last reply
        0
        • B BlueIshDan

          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 Offline
          P Offline
          Patrice T
          wrote on last edited by
          #4

          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

          B 2 Replies Last reply
          0
          • P Patrice T

            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

            B Offline
            B Offline
            BlueIshDan
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • P Patrice T

              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

              B Offline
              B Offline
              BlueIshDan
              wrote on last edited by
              #6

              I'm blown away by N= N * Base + P - 1. Its going to take me a bit of figuring out to learn this! :)

              P 1 Reply Last reply
              0
              • B BlueIshDan

                I'm blown away by N= N * Base + P - 1. Its going to take me a bit of figuring out to learn this! :)

                P Offline
                P Offline
                Patrice T
                wrote on last edited by
                #7

                Run it on debugger and track how variables evolves.

                Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

                1 Reply Last reply
                0
                • B BlueIshDan

                  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)
                  ' ------------------------------------- '
                  
                  ' ---------------------
                  
                  P Offline
                  P Offline
                  Patrice T
                  wrote on last edited by
                  #8

                  And the simplified second function:

                  Public Function ConvertNumberToDefinition(ByVal number As Variant, _
                  ByVal def As String) As String

                  Dim 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

                  B 1 Reply Last reply
                  0
                  • P Patrice T

                    And the simplified second function:

                    Public Function ConvertNumberToDefinition(ByVal number As Variant, _
                    ByVal def As String) As String

                    Dim 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

                    B Offline
                    B Offline
                    BlueIshDan
                    wrote on last edited by
                    #9

                    Patrice, Thank you very much for your guidance! Rest can be assured that I am studying from these and learning. Kindest Regards Daniel :)

                    P 1 Reply Last reply
                    0
                    • B BlueIshDan

                      Patrice, Thank you very much for your guidance! Rest can be assured that I am studying from these and learning. Kindest Regards Daniel :)

                      P Offline
                      P Offline
                      Patrice T
                      wrote on last edited by
                      #10

                      have fun with the code :)

                      Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups