So how do i go about converting a string to a binary sequence, i need this to securely transmit strings over rednet.
thank you in advance.
Convert String To Binary?
Started by bfbc2jb, Aug 21 2013 10:31 PM
9 replies to this topic
#1
Posted 21 August 2013 - 10:31 PM
#2
Posted 21 August 2013 - 11:01 PM
You could use string.byte on each character in the string and calculate the binary equivalent of that decimal number.
local function decimalToBinary (number)
-- Put your code here. There is more than one algorithm for this :P/>
end
local _string = "Test."
local bin = ""
for character in _string:gmatch ('.') do
bin = bin .. decimalToBinary (character:byte())
end
#3
Posted 21 August 2013 - 11:05 PM
You can use string.byte to know the value of a char. After that you can convert it to binary or encode it, there is many ways.
Here I give you Decimal to Binary and Binary to Decimal. I use that in one of my program.
Note: When I posted this, I didn't saw Grim Reaper message because I was creating this message when he post.
Here I give you Decimal to Binary and Binary to Decimal. I use that in one of my program.
function DB(dec)
local result = ""
repeat
local divres = dec / 2
local int, frac = math.modf(divres)
dec = int
result = math.ceil(frac) .. result
until dec == 0
local StrNumber
StrNumber = string.format(result, "s")
local nbZero
nbZero = 16 - string.len(StrNumber)
local Sresult
Sresult = string.rep("0", nbZero)..StrNumber
return Sresult
end
function BD(bin)
local num = 0
local ex = string.len(bin) - 1
local l = 0
l = ex + 1
for i = 1, l do
b = string.sub(bin, i ,i)
if b == "1" then
num = num + 2^ex
end
ex = ex - 1
end
--return string.format("%u", num) -- If you want a string
return num
end
Note: When I posted this, I didn't saw Grim Reaper message because I was creating this message when he post.
#5
Posted 22 August 2013 - 01:13 AM
That's cool, man. You did offer a more thorough solution than I did, after all
#6
Posted 22 August 2013 - 07:19 AM
Grim Reaper, on 21 August 2013 - 11:01 PM, said:
You could use string.byte on each character in the string and calculate the binary equivalent of that decimal number.
local function decimalToBinary (number)
-- Put your code here. There is more than one algorithm for this :P/>/>/>
end
local _string = "Test."
local bin = ""
for character in _string:gmatch ('.') do
bin = bin .. decimalToBinary (character:byte())
end
#7
Posted 22 August 2013 - 11:37 AM
all of these are excellent suggestions, except for one minor thing. Lua can already do this natively.
tonumber takes an optional 2nd argument, the base of the string to convert.
ex, tonumber("10101",2) would give you 21.
The 2nd arg can be up to base 36 (0-1 plus A-Z).
tonumber takes an optional 2nd argument, the base of the string to convert.
ex, tonumber("10101",2) would give you 21.
The 2nd arg can be up to base 36 (0-1 plus A-Z).
#8
Posted 22 August 2013 - 12:52 PM
Unfortunately, I don't think that lua's tonumber can convert from decimal into binary. Generally, tonumber is converting its first parameter into a lua number which is a double, so I don't think that tonumber converts from base 10 into any other base.
For example, running this code in the lua command line:
However, I'm not entirely sure if this is correct or not. I might just be confusing the parameters when converting from decimal to binary
Here's a function written by Jajnick to convert a decimal number into a binary string.
For example, running this code in the lua command line:
print (tonumber ("5", 2))
prints nil.However, I'm not entirely sure if this is correct or not. I might just be confusing the parameters when converting from decimal to binary
Here's a function written by Jajnick to convert a decimal number into a binary string.
function bin(dec) local result = "" repeat local divres = dec / 2 local int, frac = math.modf(divres) dec = int result = math.ceil(frac) .. result until dec == 0 return result endFrom his api: http://www.computerc...nd-hexadecimal/
#9
Posted 22 August 2013 - 01:46 PM
Yeah, you'll still need a function to convert a number to a binary string. Jajnick's looks good, here's another I've used before...
local hexToBin_lut={ ["0"]="0000", ["1"]="0001", ["2"]="0010", ["3"]="0011", ["4"]="0100", ["5"]="0101", ["6"]="0110", ["7"]="0111", ["8"]="1000", ["9"]="1001", ["a"]="1010", ["b"]="1011", ["c"]="1100", ["d"]="1101", ["e"]="1110", ["f"]="1111", }
local function toBinary(num)
return string.format("%x",num):gsub(".",function(v) return hexToBin_lut[v] end):match("^[0]*(%d*)$")
end
#10
Posted 29 August 2014 - 11:32 PM
code
I wrote this literally today. Noticing this post is about a week old, I hope it's still helpful.EDIT: Oh my God is it really 2014? I'M SORRY FOR NECROING THIS
Edited by Alice, 29 August 2014 - 11:33 PM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











