convert a number to binary with lua ?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Tuxion
Prole
Posts: 14
Joined: Mon Jan 08, 2018 6:31 pm

convert a number to binary with lua ?

Post by Tuxion »

I would like to know how to convert a number to binary with lua.

Anybody have any idea how we do it?
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: convert a number to binary with lua ?

Post by grump »

Integers or any number?
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: convert a number to binary with lua ?

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Tuxion
Prole
Posts: 14
Joined: Mon Jan 08, 2018 6:31 pm

Re: convert a number to binary with lua ?

Post 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
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: convert a number to binary with lua ?

Post 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?
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: convert a number to binary with lua ?

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Tuxion
Prole
Posts: 14
Joined: Mon Jan 08, 2018 6:31 pm

Re: convert a number to binary with lua ?

Post 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.
BorhilIan
Prole
Posts: 38
Joined: Wed Mar 15, 2017 6:46 am

Re: convert a number to binary with lua ?

Post 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
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: convert a number to binary with lua ?

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Tuxion
Prole
Posts: 14
Joined: Mon Jan 08, 2018 6:31 pm

Re: convert a number to binary with lua ?

Post by Tuxion »

I'm just trying to display the conversion of my RVBA values to binary. for example 255 to 11111111.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 153 guests