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

API: Letter to Binary Converter

Started by DaKodiMaster, 03 April 2015 - 01:58 PM
DaKodiMaster #1
Posted 03 April 2015 - 03:58 PM
Note: This project is now discontinued.
Edited on 25 May 2016 - 12:32 AM
Lyqyd #2
Posted 03 April 2015 - 04:07 PM
You might be better off using string.byte and a number-to-binary-representation-string converter. You could probably shorten this down to a dozen lines, likely less.
H4X0RZ #3
Posted 03 April 2015 - 08:18 PM
If you want to write something like that, take a look at my API http://pastebin.com/qPaeUQRa
For you, only binaryToNumber and numberToBinary will be interesting I think, the other stuff is just some binary logic stuff.

Also my API got a problem when you try to do logic on strings with an un-equal length, because it will fill the shorter string up with "0000000" until they are the same length.

(here is a compressed version of it http://pastebin.com/iwtWn6yG the file size is only reduced by roughly 50%.)
Edited on 03 April 2015 - 06:24 PM
Anavrins #4
Posted 03 April 2015 - 08:49 PM
Wow, this has no uses for bit operations, I first thought it would return a table such as {1,0,0,0,0,0,1}
but it literally print() "A = Binary: 01000001"
DaKodiMaster #5
Posted 29 December 2015 - 02:04 PM
I'm working on another version of this, it will return a table such as: {0,0,1,1,0,0,0,0} for 0 rather than a string.
Pyuu #6
Posted 31 December 2015 - 07:16 PM
No support for squiggly (or as nerds say: "tilde"), I'm sad.
Also, instead of making a table with binary conversions, I recommend this way:

SpoilerTake a byte (1 length string) and do string.byte on it to get the number correlating to the letter.
The number then can be taken through a loop to get an enumerator table, in binary we have: 128,64,32,16,8,4,2,1. The fun thing about binary is that it is predictable based on what number you have. For example, 127 is equal to 64+32+16+8+4+2+1, so we can immediately tell that because 127 is less than 128, that the first bit is 0.
But what if it is: 254? If we check if 254 is >= 128, then it will be true, then 254 >= 64 etc. So, to perform the check effectively, we subtract 128 if it is true.

So,


vNum = string.byte("a") -- 97
bits = {}
if vNum >= 128 then
bits[1] = true
vNum = vNum - 128
else
bits[1] = false
end

Now, we just have to repeat this process through all 8 bits. Which can be sort of tedious written out.

vNum = string.byte("a") -- 97
bits = {}
if vNum >= 128 then
bits[1] = true
vNum = vNum - 128
else
bits[1] = false
end
if vNum >= 64 then
bits[2] = true
vNum = vNum - 64
else
bits[2] = false
end
if vNum >= 32 then
bits[3] = true
vNum = vNum - 32
else
bits[3] = false
end
if vNum >= 16 then
bits[4] = true
vNum = vNum - 16
else
bits[4] = false
end
if vNum >= 8 then
bits[5] = true
vNum = vNum - 8
else
bits[5] = false
end
if vNum >= 4 then
bits[6] = true
vNum = vNum - 4
else
bits[6] = false
end
if vNum >= 2 then
bits[7] = true
vNum = vNum - 2
else
bits[7] = false
end
if vNum >= 1 then
bits[8] = true
vNum = vNum - 1
else
bits[8] = false
end

The above code is a mess, and there is a pattern to it as well. The 128,64,32,16,8,4,2,1 pattern can be rewritten as 2^7 , 2^6 , 2^5 , 2^4 , 2^3 , 2^2 , 2^1 , 2^0. So it is possible to for loop this entire process. So let's rewrite this.


vNum = string.byte("a") -- 97
bits = {}
for i=1, 8 do
if vNum >= 2^(i-1) then
bits[i] = true
vNum = vNum - 2^(i-1)
else
bits[i] = false
end
end
-- bytes = { false, true, true, false, false, false, false, true } -- 0110 0001

Which, this method completely removes the need for an external table of contents for which character = what in binary, it also is small and compact and also can be used for enumerators / char to binary strings.

If you're wondering what an enumerator is, it's basically a way to store 8 booleans in a single byte.