If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
Ruby binary to decimal
3 posts
• Page 1 of 1
Ruby binary to decimal
Hi,
Just wondered if ruby can convert binary number to decimal and vice versa as a part of internal code. Why is that needed?
Because considering the fact that seq's should store presets in huge amount of arrays, it would be alot better to feed the seq's ruby code with decimal value rather than feed it with array, don't it? For instance, the decimal value for the 16 parameters array 0111001110100111 is 29607, which could be stored as a simple string
Possible?
Just wondered if ruby can convert binary number to decimal and vice versa as a part of internal code. Why is that needed?
Because considering the fact that seq's should store presets in huge amount of arrays, it would be alot better to feed the seq's ruby code with decimal value rather than feed it with array, don't it? For instance, the decimal value for the 16 parameters array 0111001110100111 is 29607, which could be stored as a simple string
Possible?
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: Ruby binary to decimal
In Ruby you can do number base conversions by passing the base as an argument to the "to_s" string converting method, and the 'to_i' integer converting method - like this...
When you need to store a lot of true/false flags this can be very useful - but you need to think it through carefully before using it, or you could end up making things less efficient.
- For 'green' values, you only have 31 bits to play with (the last bit is used for the sign)
- You can't store integers as VST presets, only floats. Floats have a wider number range, but only 24 bits of precision - once you go above a 24bit integer, they will start skipping values.
- Strings are really just another kind of array - an array of characters. They will take less memory, as characters are smaller (1 byte) than floats or integers (4 bytes - until we get 64 bit!). But accessing a particular character and testing it won't necessarily be any more efficient than just using an array to start with.
- Integer <-> String conversions take time and CPU load!
However, yes, this is an interesting one because of how Ruby works. In Ruby, every value is some kind of Ruby "object" - and those aren't stored in memory the same way as the green data types. So whenever you change between Green <-> Ruby, there's a conversion going on.
When you change a 'green' array to Ruby, or back again, the conversion will take much more processing - because every single item in the array has to be converted. So you're right, passing a single integer into or out of Ruby will be much more efficient (so long as other things don't eat up what you just saved, of course!)
If you are passing a value between two RubyEdits, you can bypass this conversion by using the white 'Ruby Value' value links - but that isn't always what you want for the rest of the schematic.
- Code: Select all
bin_to_int = "1101".to_i(2) # returns 13
int_to_bin = 13.to_s(2) # returns "1101"
hex_to_int = "1d".to_i(16) # returns 29
int_to_hex = 29.to_s(16) # returns "1d"
When you need to store a lot of true/false flags this can be very useful - but you need to think it through carefully before using it, or you could end up making things less efficient.
- For 'green' values, you only have 31 bits to play with (the last bit is used for the sign)
- You can't store integers as VST presets, only floats. Floats have a wider number range, but only 24 bits of precision - once you go above a 24bit integer, they will start skipping values.
- Strings are really just another kind of array - an array of characters. They will take less memory, as characters are smaller (1 byte) than floats or integers (4 bytes - until we get 64 bit!). But accessing a particular character and testing it won't necessarily be any more efficient than just using an array to start with.
- Integer <-> String conversions take time and CPU load!
kortezzzz wrote:better to feed the seq's ruby code with decimal value rather than feed it with array, don't it?
However, yes, this is an interesting one because of how Ruby works. In Ruby, every value is some kind of Ruby "object" - and those aren't stored in memory the same way as the green data types. So whenever you change between Green <-> Ruby, there's a conversion going on.
When you change a 'green' array to Ruby, or back again, the conversion will take much more processing - because every single item in the array has to be converted. So you're right, passing a single integer into or out of Ruby will be much more efficient (so long as other things don't eat up what you just saved, of course!)
If you are passing a value between two RubyEdits, you can bypass this conversion by using the white 'Ruby Value' value links - but that isn't always what you want for the rest of the schematic.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: Ruby binary to decimal
Thanks trog. So, if I understand well, seems like we will need it. For instance, in very large designs when a really huge array is needed like sequencers with lot of bars. In that case, cpu loading time is really secondary to the benefits.
Will definitely try it. thanks
Will definitely try it. thanks
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
3 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 34 guests