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

Converting to base 2 (aka binary)

Started by Pharap, 15 October 2012 - 11:48 PM
Pharap #1
Posted 16 October 2012 - 01:48 AM
Ok, so I decided to try out the second ability of the tonumber() function, converting between bases.
Yet when I try to convert a number to base 2 and print it, it either comes up as nil or a blank string.

Is converting to base 2 inherently broken or am I doing something wrong?

print(tonumber(16,2)) –prints empty string
print(tostring(tonumber(16,2))) –prints nil

Thank you for your time.
brett122798 #2
Posted 16 October 2012 - 01:57 AM
Oh, no. You have it all wrong, 'tonumber()' just converts a string to a number and vice versa with 'tostring()'. Here's an example(with 'tostring()'):


rednet.open("left")

mynumber = 512
mynumberstring = tostring(mynumber)
rednet.send(1, mynumberstring)
GopherAtl #3
Posted 16 October 2012 - 01:58 AM
You're just thinking backwards. tonumber takes a string and turns it into a number, treating the starting string as the specified base.

To convert a number into a string representation with a specified base, use string.format("%b",myNum). To force some number of bits to display, with leading zeros, make the format string "%08b", where 8 is the number of bits.
MysticT #4
Posted 16 October 2012 - 02:04 AM
You're just thinking backwards. tonumber takes a string and turns it into a number, treating the starting string as the specified base.

To convert a number into a string representation with a specified base, use string.format("%b",myNum). To force some number of bits to display, with leading zeros, make the format string "%08b", where 8 is the number of bits.
Well, there's no %b format, so you'll have to make your own function to do this. Using the bit api will help you.
GopherAtl #5
Posted 16 October 2012 - 02:11 AM
Isn't there? Sorry, I had only used %x, just assumed lua's format supported b as well. And the tobits method on bit was removed in 1.42 when it became binary, I think, so… Yeah, gotta just roll your own I guess :/
Pharap #6
Posted 16 October 2012 - 03:37 AM
Oh, no. You have it all wrong, 'tonumber()' just converts a string to a number and vice versa with 'tostring()'. Here's an example(with 'tostring()'):
You're just thinking backwards. tonumber takes a string and turns it into a number, treating the starting string as the specified base.

For all those people saying I am wrong and do not know about tonumber()'s second function:

http://pgl.yoyo.org/luai/i/tonumber
http://www.wowwiki.com/API_tonumber

This didn't just happen overnight, and it does appear to work with other bases for example:

print(tostring(tonumber(345,16))) –prints 837

Try it yourself, I'm not completely crazy.
Fatal_Exception #7
Posted 16 October 2012 - 03:46 AM
That's telling it you have a number in base 16 that you want to display in base 10.
GopherAtl #8
Posted 16 October 2012 - 03:47 AM
Take another look at those numbers. It's taking the parameter, making it a string, then interpreting that string as base 16, which is not what you're trying to do.
BrolofTheViking #9
Posted 16 October 2012 - 03:50 AM
This function I wrote will return the number converted to binary, although the computer will not recognize that it is in binary, and thus you will probably need to write another function to decode it, but I hope it helps.

function toBinary(number)
a = 1
b = 1
while (number / a ) > 1 do
  a = a * 2
  b = b * 10
end
if number / a ~= 1 then
  a = a / 2
  b = b / 10
end
out = 0
while a >= 1 do
  c = math.floor(number / a) * b
  out = out + c
  number = number - (math.floor(number / a) * a)
  a = a / 2
  b = b / 10
end
return out
end
Pharap #10
Posted 16 October 2012 - 03:54 AM
That's telling it you have a number in base 16 that you want to display in base 10.

Right, that's my issue then.
Thanks for that.

Bit irksome that they only coded a one way sytstem, this means I'm going to have to bust out the ugly binary conversion functions, but hey-ho, you can't have it all.


This function I wrote will return the number converted to binary, although the computer will not recognize that it is in binary, and thus you will probably need to write another function to decode it, but I hope it helps.

Thanks, but I do already have one, I ported my C# one ages ago and it works fine, I was just hoping for something a bit more portable, unfortunately it seems lua has let me down this time.