Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
A neat function - inbuilt into Lua — Gideros Forum

A neat function - inbuilt into Lua

OZAppsOZApps Guru
edited April 2012 in General questions
if you have to convert numbers in various base, like decimal, octal, hex, including binary, then you can use the nifty lua inbuilt function tonumber, this shall beat writing your own functions to convert a number.
theNumber="11"
--Binary 11 = 3
print(tonumber(theNumber,2))
 
--Octal 011 = 9
print(tonumber(theNumber,8))
 
--Decimal 11 = 11
 
--Hex 0x11 = 17
print(tonumber(theNumber,16))
So hope that you could use this neat trick somewhere in your apps...

Dislikes: keszegh

twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
+1 -1 (+0 / -1 )Share on Facebook

Comments

  • ScouserScouser Guru
    edited April 2012
    In the Hitchhikers Guide To The Galaxy books, Arthur Dent asked the question What is the answer to the ultimate question of life, the universe and everything?
    The answer he received was 42 but was told he didn't really understand the answer because he didn't know what the ultimate question was.

    Near the end of the book relying on his subconscious mind he drew Scrabble letters out of a bag to try and reveal the ultimate question once and for all. The letters he drew out revealed the question What do you get if you multiply six by nine?
    We know this is 54 so how are the 2 answers connected?
    local answer = "42"
     
    print("What is the answer to the ultimate question of life, the universe and everything? "..answer)
     
    answer = tonumber(answer,13)
     
    print("What do you get if you multiply six by nine? "..answer)
    What is the answer to the ultimate question of life, the universe and everything? 42
    What do you get if you multiply six by nine? 54
    There you go, I found a use for it :))
  • techdojotechdojo Guru
    edited April 2012
    @Scouser - I'm disappointed in you, you know that when this theory was suggested to Adam's in a Q&A session he was quoted as saying "I don't do jokes is base 13"

    :))

    Likes: WauloK

    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
    +1 -1 (+1 / -0 )Share on Facebook
  • To add to this, I did a bit of search, and from that I made a function that can work the other way:
    to Call it: DecToBase(number,base) so if you want a Binary output, place 2 in the base, hex, place 15 etc. I needed 4, as I am doing a Algorythm to validate 4 slots of 4 possibilities, and need to count on each column. Would be nice if this could be added to Compiled function list....
    function DecToBase(IN,BASE)
        local B,K,OUT,D=BASE or 10,"0123456789ABCDEFGHIJKLMNOPQRSTUVW",""
        while IN>0 do
            IN,D=math.floor(IN/B),math.mod(IN,B)+1
            OUT=string.sub(K,D,D)..OUT
        end
        return OUT
    end
    REAL programmers type copy con filename.exe
    ---------------------------------------
Sign In or Register to comment.