Jump to content




Convert String To Binary?


  • You cannot reply to this topic
9 replies to this topic

#1 bfbc2jb

  • Members
  • 6 posts

Posted 21 August 2013 - 10:31 PM

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.

#2 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

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 IHHI

  • Members
  • 9 posts

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.

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.

#4 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 22 August 2013 - 12:41 AM

View PostIHHI, on 21 August 2013 - 11:05 PM, said:

Note: When I posted this, I didn't saw Grim Reaper message because I was creating this message when he post.

That, my friend, is called being ninja'd :)

#5 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

Posted 22 August 2013 - 01:13 AM

That's cool, man. You did offer a more thorough solution than I did, after all ;)

#6 bfbc2jb

  • Members
  • 6 posts

Posted 22 August 2013 - 07:19 AM

View PostGrim 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
thanks, also clueless i tried your decimal to binary code and it just does nothing, is there anything im missing?

#7 GopherAtl

  • Members
  • 888 posts

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).

#8 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

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:
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 :P

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
end
From his api: http://www.computerc...nd-hexadecimal/

#9 GopherAtl

  • Members
  • 888 posts

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 Alice

  • Members
  • 429 posts
  • LocationBehind you.

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