|
| 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 |
|
|
|
| 17 Sep 2012 10:13 PM |
| tonumber("00101101", 2) -- > 45 |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 17 Sep 2012 10:43 PM |
| And that is the end of the challenge. |
|
|
| Report Abuse |
|
|
|
| 18 Sep 2012 12:07 AM |
| I meant using an actual formula/ |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| 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
|
  |
| Joined: 29 Oct 2011 |
| Total Posts: 320 |
|
| |
|
HotThoth
|
  |
 |
| 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 |
|
|