Convert base 10 to base 36 numbers using numbers 0 - 9 and letters A - Z

Hi all,

We are looking for a function that converts base 10 numbers to base 36 numbers (using numbers and letters to represent base 36 numbers), and also another function to convert base 36 text 

Example1 - Convert base 10 to base 36 >> enter base 10 number int(3634) and get base 36 text string "2SY"

Example2 - Convert base 36 to base 10 >> enter base 36 text string("A2J") and get base 10 number int 13051

Does anyone have an idea where we can start from?

Thanks

  Discussion posts and replies are publicly visible

Parents
  • +2
    Certified Lead Developer

    For anyone who's interested, I was able to whip up this Appian expression rule last night, which uses 100% out-of-box functionality.  In it, a user can pass any valid integer, as well as any base (between 2 and 36), and get the resulting conversion in a string.  It performs fairly well, converting some of the largest allowable Appian integers into base 36 in only 2ms.

    if(
      or(
        isnull(ri!base),
        isnull(ri!integer),
        ri!base > 36,
        ri!base < 2
      ),
      null(),
      
      with(
        local!base: ri!base,
        
        local!charset: left("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", local!base),
        
        local!powerBase: log(ri!integer, local!base),
        local!numCharacters: ceiling(local!powerBase) + 1, /* the resulting number will need up to this many characters */
        
        
        local!strings: a!forEach(
          items: enumerate(local!numCharacters),
          
          expression: with(
            local!placeValue: ri!integer / (local!base ^ (fv!item)), /* requires zero-indexed list */
            
            local!charIndex: floor(mod(local!placeValue, local!base)),
            
            if(
              local!placeValue < 1,
              {},
              local!charset[local!charIndex+1]
            )
            
          )
        ),
        
        concat(reverse(local!strings))
        
        /* debug -- uncomment the following lines to see earlier calculated values */
        /*&char(10) & "power base: " & local!powerBase*/
        /*&char(10)& "places: " & local!numCharacters*/
      )
    )

    To convert back from a given base to base10 would just require an expression rule that does the reverse of this, which IMHO would actually be easier (edit: i've written this too and posted it below).

  • +1
    Certified Lead Developer
    in reply to nanfak

    Also since it was bugging me, I went ahead and created the "convert back to base 10" expression rule, which takes any positive baseX value (between base 2 and base 36), along with the specific base in question.  Note it does minimal error handling but probably can't handle extended corner cases.

    if(
      or(
        isnull(ri!xBaseNumberString),
        isnull(ri!base),
        ri!base > 36,
        ri!base < 2
      ),
      null(),
      
      with(
        local!charset: left("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", ri!base),
        local!errorValue: -1,
        
        local!numCharacters: len(ri!xBaseNumberString),
        
        local!valueArray: a!forEach(
          items: reverse(enumerate(local!numCharacters)),
          
          expression: with(
            local!char: upper(ri!xBaseNumberString[fv!item + 1]),
            
            local!charPosition: find(local!char, local!charset),
            local!charValue: local!charPosition - 1,
            
            /* output: */
            if(
              local!charPosition = 0, /* try to handle the case of the number string having invalid characters */
              local!errorValue,
              local!charValue * (ri!base ^ (fv!index-1))
            )
          )
        ),
        
        if(
          contains(
            local!valueArray,
            local!errorValue
          ),
          tointeger(null()),
          sum(local!valueArray)
        )
        
        /* debug -- uncomment the following lines to see earlier calculated values */
        /*&char(10) & "numchars: " & local!numCharacters*/
      )
    )

  • 0
    A Score Level 1
    in reply to Mike Schmitt
    Thank you Mike Schmitt.

    We truly appreciate this.
Reply Children
No Data