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

bitWise API (advanced string, bit, byte conversion)

Started by zxci, 25 November 2015 - 10:48 AM
zxci #1
Posted 25 November 2015 - 11:48 AM
I realized LUA did not have the required capabilites of converting into complete binary, so I made my own API.
LUA can utilize string.byte to transform "H" to "104" but it can not convert directly to bits, or so I could not find.

Capabilities/functions:
byte2binary:
Pass it a single byte (such as 104) and it will convert it into its corresponding 8 bit binary number (such as 01101000).
binary2byte:
Pass it a complete binary number (max. 8 digits, example 01101000) and it will convert it to the corresponding byteform (104)/
string2bytes:
Pass it a string (such as "HHH") and it will break it up into an array of bytes, such as ("104104104".
string2binary:
Pass it a string (such as "HHH") and it will convert it into a binary string ("011010000110100001101000")
bytes2stringArr:
Pass it an array of bytes ({"104","104"}) and it will convert it into an array of strings ({"H","H"})
binary2string:
Pass it a full binary string (011010000110100001101000) and it will convert it into an ascii string ("HHH")
The capabilities to further split these strings or arrays into desired divisions are easily implemented, but I don't need them, so I didn't.

113 lines of code without the leading comments
http://pastebin.com/Bw9rpLPn

this probably isn't well optimized, by the way, this is just what I threw together to suit my needs at 5am..
Edited on 25 November 2015 - 08:49 PM
H4X0RZ #2
Posted 25 November 2015 - 05:21 PM
Nice! But why are you converting like this? (Many if blocks; fixed size)

Here is something I wrote some time ago
http://pastebin.com/qPaeUQRa
See how easy it can be? ;D
zxci #3
Posted 25 November 2015 - 09:48 PM
thank you, and to answer the question mostly because my implementation will always be dealing with full 8-block sequences.

I'm actually not very good at manipulating strings either, it was easier to just assume it'll all be a fixed length or a multiple of that fixed length lol