Page 1 of 2

convert a number to binary with lua ?

Posted: Fri Mar 09, 2018 1:37 pm
by Tuxion
I would like to know how to convert a number to binary with lua.

Anybody have any idea how we do it?

Re: convert a number to binary with lua ?

Posted: Fri Mar 09, 2018 2:07 pm
by grump
Integers or any number?

Re: convert a number to binary with lua ?

Posted: Fri Mar 09, 2018 2:16 pm
by zorg
Also, if we're talking about negatives as well, what representation would you want for those? the "standard" 2-s complement? Others are also possible.

Re: convert a number to binary with lua ?

Posted: Fri Mar 09, 2018 2:32 pm
by Tuxion
"Integers or any number?"

both

"Also, if we're talking about negatives as well, what representation would you want for those? the "standard" 2-s complement? Others are also possible."

all

Re: convert a number to binary with lua ?

Posted: Fri Mar 09, 2018 2:45 pm
by grump
Um... so you want a 2 functions; one that converts any integer to binary, and another one that does the same with floating point numbers? And you want the signed numbers to be converted to every possible representation?

Is IEEE 754 okay, or do you require the floating point numbers in every possible representation too?

Re: convert a number to binary with lua ?

Posted: Fri Mar 09, 2018 2:45 pm
by zorg
Well, you could use the FFI and cast lua numbers to C data types, and write those out as strings; i think that might work.
An union with members being a 32bit signed integer and a 64bit double could also be more versatile... haven't tested this for downsides though, but i remember reading something about this...
Without the FFI, it's kind of complicated, and slow, although technically they are binary already, they are doubles. If you want to output strings of 0s and 1s, that's again different.

Re: convert a number to binary with lua ?

Posted: Fri Mar 09, 2018 3:19 pm
by Tuxion
I'm sorry if I don't ask the right questions, I'm actually a big noob in programming and I'm just getting started.

I was just trying to find out how to code a function that converted an entire number into binary, but I was curious about other solutions.

for the rest, honestly I don't know FFI or IEEE 754.

Re: convert a number to binary with lua ?

Posted: Sat Mar 10, 2018 5:46 am
by BorhilIan
As far as integers go i asked a similar question a while back, wound up making this thing which includes function for to/from binary.
viewtopic.php?f=5&t=83752

Re: convert a number to binary with lua ?

Posted: Sat Mar 10, 2018 6:31 am
by zorg
The question still remains vague as hell though; different methods are sufficient only for different end-goals:
- To show values in the binary numerical system from a specific format (like "integers", or IEEE floats/doubles, or even characters based on any encoding)
- To write out values in the most compact of ways to either files or memory
I'm guessing we could help more if you actually told us what you want to do / use this for; keep in mind, all values are, in a technical sense, binary, at all times, they are just being interpreted differently; your computer only ever uses 0s and 1s, it's just abstracted away from you.

Re: convert a number to binary with lua ?

Posted: Sat Mar 10, 2018 10:43 am
by Tuxion
I'm just trying to display the conversion of my RVBA values to binary. for example 255 to 11111111.