Announcement

Collapse
No announcement yet.

base_convert — Convert a number between arbitrary bases

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • base_convert — Convert a number between arbitrary bases

    i am converting a php script to lua but having problems with base_convert — Convert a number between arbitrary bases

    PHP Code:
    string base_convert string $number int $frombase int $tobase 
    what would be the lua version.
    Plugins or Sources MokoX
    BunnyHop 2021 Here

  • #2
    After a quick search I found this, the script might work as a starting point.

    Ulrich

    Comment


    • #3
      Originally posted by Ulrich View Post
      After a quick search I found this, the script might work as a starting point.

      Ulrich
      I will give this a look over, the problem with others I see do far will only take 2 values not 3 but from what I read of the funtion it was no auto detecting what the base it was given so I was not sure how to use them as they was no documents for them, I give this a look thanks.
      Plugins or Sources MokoX
      BunnyHop 2021 Here

      Comment


      • #4
        Is there a function that mimics the php split functions I have one that someone created but does not work in AMS and then one that says it needs string but php split takes table and string.
        Plugins or Sources MokoX
        BunnyHop 2021 Here

        Comment


        • #5
          split is deprecated in PHP and should not be used any more, use explode($pattern, $str).

          Code:
          function explode(div,str) -- credit: http://richard.warburton.it
            if (div=='') then return false end
            local pos,arr = 0,{}
            -- for each divider found
            for st,sp in function() return string.find(str,div,pos,true) end do
              table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
              pos = sp + 1 -- Jump past current divider
            end
            table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
            return arr
          end
          From: http://lua-users.org/wiki/MakingLuaLikePhp

          I used to have a base conversion function in Lua somewhere, but I can't seem to find it right now...
          Bas Groothedde
          Imagine Programming :: Blog

          AMS8 Plugins
          IMXLH Compiler

          Comment


          • #6
            Originally posted by Imagine Programming View Post
            split is deprecated in PHP and should not be used any more, use explode($pattern, $str).

            Code:
            function explode(div,str) -- credit: http://richard.warburton.it
              if (div=='') then return false end
              local pos,arr = 0,{}
              -- for each divider found
              for st,sp in function() return string.find(str,div,pos,true) end do
                table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
                pos = sp + 1 -- Jump past current divider
              end
              table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
              return arr
            end
            From: http://lua-users.org/wiki/MakingLuaLikePhp

            I used to have a base conversion function in Lua somewhere, but I can't seem to find it right now...
            I am converting a script to work in AMS, but if that gives the same result I happly us it
            Plugins or Sources MokoX
            BunnyHop 2021 Here

            Comment


            • #7
              explode does the same as split, it will slice a string into pieces based on a specified delimiter. :yes

              As for base_convert, between which bases do you need to be able to convert?
              Bas Groothedde
              Imagine Programming :: Blog

              AMS8 Plugins
              IMXLH Compiler

              Comment


              • #8
                Originally posted by Imagine Programming View Post
                explode does the same as split, it will slice a string into pieces based on a specified delimiter. :yes

                As for base_convert, between which bases do you need to be able to convert?
                32 to 10 so far its used a few times so not sure converting it bit by bit at the moment
                and I am sure there is back to 10 from 32 at some point.
                Plugins or Sources MokoX
                BunnyHop 2021 Here

                Comment


                • #9
                  I still have an old base conversion class in PHP which I will try to translate to Lua. It will be improvisation, however it should do the trick for like every possible base.

                  One note though, the base.convert I will write will be case sensitive. 4az9 ~= 4Az9.
                  Bas Groothedde
                  Imagine Programming :: Blog

                  AMS8 Plugins
                  IMXLH Compiler

                  Comment


                  • #10
                    There ya go, test this well!
                    Attached Files
                    Bas Groothedde
                    Imagine Programming :: Blog

                    AMS8 Plugins
                    IMXLH Compiler

                    Comment


                    • #11
                      Originally posted by Imagine Programming View Post
                      There ya go, test this well!
                      That I will sir that I will thank you very much I will enjoy learning from this
                      Plugins or Sources MokoX
                      BunnyHop 2021 Here

                      Comment


                      • #12
                        Originally posted by kingzooly View Post
                        That I will sir that I will thank you very much I will enjoy learning from this
                        :yes ugh 10-char limit
                        Bas Groothedde
                        Imagine Programming :: Blog

                        AMS8 Plugins
                        IMXLH Compiler

                        Comment

                        Working...
                        X