generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Lua Challenge(meh): Binary to Decimal

Previous Thread :: Next Thread 
veerstrong is not online. veerstrong
Joined: 23 Jun 2009
Total Posts: 2450
17 Sep 2012 09:40 PM
I am gonna try to basically make a scripting that converts Binary to Decimal, however, first off, I need help with one thingy..
How would I assign each number a place value?
Like how in '10'
The 0 is 1
The 1 is 2
And if it was '010'
It would be
1
2
4
So, how do I give those values in order to each number I put?

--i sold the lemonz f00!--
Report Abuse
NecroBumpist is not online. NecroBumpist
Joined: 12 Sep 2010
Total Posts: 4198
17 Sep 2012 10:13 PM
tonumber("00101101", 2) -- > 45
Report Abuse
Quenty is not online. Quenty
Joined: 03 Sep 2009
Total Posts: 9316
17 Sep 2012 10:43 PM
And that is the end of the challenge.
Report Abuse
veerstrong is not online. veerstrong
Joined: 23 Jun 2009
Total Posts: 2450
18 Sep 2012 12:07 AM
I meant using an actual formula/
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
18 Sep 2012 12:14 AM
local n = 0
local s = "001111001010010001101010"
for i = 1,#s do
n = n + ((s:sub(i,i)=='1')and 2^(#s-n) or 0)
end
print(n)
Report Abuse
XiJennyX is not online. XiJennyX
Joined: 29 Oct 2011
Total Posts: 320
18 Sep 2012 11:50 AM
xD
Report Abuse
HotThoth is not online. HotThoth
Forum Moderator
Joined: 24 Aug 2010
Total Posts: 1176
18 Sep 2012 04:15 PM
Think of it this way:

Whatever your base is, the very right-most digit is always the 1's digit, and every place to the left of it has the value of the previous place times your base. So for base 10, reading from right to left you get:

4023 --> 3 of value 1 + 2 of value 1*10 + 0 of value 1*10*10 + 4 of value 1*10*10*10.

For base 9, this is just:
4023 --> 3 of value 1 + 2 of value 1*9 + 8 of value 1*9*9 + 4 of value 1*9*9*9

So if you have a loop like this:
numberToConvert = "4023"
base = 9

baseValue = 1
total = 0
for each digit in numberToConvert from right to left do:
total = total + baseValue * digit
baseValue = baseValue * base
end

Then you would get 4023 in base 9. This is just pseudocode to give you the idea how to do it (and keep things relatively readable). Hopefully this helps explain things!


- HotThoth
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image