This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ComputerCraftFan11's profile picture

Number to Binary (and vice versa)

Started by ComputerCraftFan11, 12 June 2012 - 07:18 AM
ComputerCraftFan11 #1
Posted 12 June 2012 - 09:18 AM
I'm making a program that can convert a lua program into Binary, then when you wanna run it it converts it back and reads it.

I currently got numbers (using binary) for the translation thing.

The code is:

local aTranslations = {}
local bTranslations = {}
function addPatch( letter, trans )
aTranslations[letter] = trans
bTranslations[trans] = letter
end
addPatch("1", "00110001") --Translate Numbers
addPatch("2", "00110010")
addPatch("3", "00110011")
addPatch("4", "00110100")
addPatch("5", "00110101")
addPatch("6", "00110110")
addPatch("7", "00110111")
addPatch("8", "00111000")
addPatch("9", "00111001")
addPatch("0", "00110000")
print("Option: (1=Number to Binary) (2=Binary to Number)")
i2nput = read()
shell.run("clear") --I like to expose people to viruses that use names like "clear" :3
if i2nput == "1" or i2nput == "2" then
write("Translate: ")
input = read()

if i2nput == "1" then
  for i=1,string.len(input) do
   if aTranslations[string.sub(input, i, i)] then
    write(aTranslations[string.sub(input, i, i)].. " ")
   else
    write(string.sub(input, i, i))
   end
  end
else
  for match in input:gmatch("[^ \t]+") do
   if bTranslations[match] then
    write(bTranslations[match])
   else
    write(match)
   end
  end
end
print() --Make another line
end
I didn't have much time so you can type "1" to translate number to binary, and "2" for vice versa.
kazagistar #2
Posted 13 June 2012 - 04:15 AM
Am I missing something? Why is binary "1" == "00110001"?
ComputerCraftFan11 #3
Posted 13 June 2012 - 04:27 AM
Am I missing something? Why is binary "1" == "00110001"?

I got it off of here.

If you translate "1", it gives you "00110001"
Maarten551 #4
Posted 13 June 2012 - 07:47 AM
Am I missing something? Why is binary "1" == "00110001"?

I got it off of here.

If you translate "1", it gives you "00110001"

Well that's ofcourse wrong, because 1 == "00000001"

Try to put the number in the 'hex' box on that website, and that should return a valid binary.
BigSHinyToys #5
Posted 13 June 2012 - 08:55 AM
the bellow may or may not be helpful

BINARY 0x101 lol :(/>/>
each bit represents double the previous value starting at 1 example
8,4,2,1
by combing different combinations of the above we can make 16 numbers from 0 to 15. this is what binary does.
this example will be working with a length or 4 bits or half a byte also know as a nibble
0 represents false,off and low. 1 represents true,on and hight.
the bellow is a list of data stating at 0 moving to 4 you should see a pattern in the bellow code.

0) = 0x0000
1) = 0x0001
2) = 0x0010
3) = 0x0011
4) = 0x0100
EXERCISE
make a value for 15, 7 and or 10
ANSWERS
Spoiler15) = 0x1111
7 ) = 0x0111
10) = 0x1010

the term value as i have used it above is also the same as decimal. but it is not hex as hex is base 16 but decimal is base 10

[EDIT]
these functions may be useful

local function expand(iInput)
local tOutput = {}
local check = 32768
for i = 1,16 do
  if iInput >= check then
   tOutput[i] = 1
   iInput = iInput - check
  else
   tOutput[i] = 0
  end
  check = check/2
end
return tOutput
end
local function compact(tInput)
local iOutput = 0
local check = 1
for i = 16,1,-1 do
  if tInput[i] == 1 then
   iOutput = iOutput + check
  end
  check = check*2
end
return iOutput
end
MysticT #6
Posted 13 June 2012 - 06:29 PM
Am I missing something? Why is binary "1" == "00110001"?

I got it off of here.

If you translate "1", it gives you "00110001"
That's the ascii value of the character "1", not the number 1.
Also, you can use this to get the number from the binary string:

local n = tonumber("10101100", 2)
It works for any base from 2 to 36 (changing the second parameter).
Bossman201 #7
Posted 13 June 2012 - 10:50 PM
I remember trying to make this program on my Ti 83+ in high school. I couldn't. My mental capacity was low.