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.
9 replies to this topic
#1
Posted 16 October 2012 - 12:48 AM
#2
Posted 16 October 2012 - 12: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)
#3
Posted 16 October 2012 - 12: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.
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.
#4
Posted 16 October 2012 - 01:04 AM
GopherAtl, on 16 October 2012 - 12:58 AM, said:
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.
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.
#5
Posted 16 October 2012 - 01: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 :/
#6
Posted 16 October 2012 - 02:37 AM
brett122798, on 16 October 2012 - 12:57 AM, said:
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()'):
GopherAtl, on 16 October 2012 - 12:58 AM, said:
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.
#7
Posted 16 October 2012 - 02:46 AM
That's telling it you have a number in base 16 that you want to display in base 10.
#8
Posted 16 October 2012 - 02: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.
#9
Posted 16 October 2012 - 02: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
#10
Posted 16 October 2012 - 02:54 AM
Fatal_Exception, on 16 October 2012 - 02:46 AM, said:
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.
BrolofTheViking, on 16 October 2012 - 02:50 AM, said:
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.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











