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

Help needed converting numbers to binary.

Started by DannySMc, 23 March 2015 - 10:16 AM
DannySMc #1
Posted 23 March 2015 - 11:16 AM
code
--lua text => binary ascii codes

function string:split(delimiter)
	local result = { }
	local from = 1
	local delim_from, delim_to = string.find( self, delimiter, from )
	while delim_from do
		table.insert( result, string.sub( self, from , delim_from-1 ) )
		from = delim_to + 1
		delim_from, delim_to = string.find( self, delimiter, from )
	end
	table.insert( result, string.sub( self, from  ) )
	return result
end

function toBits(num)
	local t={}
	while num>0 do
		rest=math.fmod(num,2)
		t[#t+1]=rest
		num=(num-rest)/2
	end
	return t
end

function makeEight( t )
	return ("0"):rep( 8 - #t ) .. t
end

function textToBinary( txt2 )
	local txt = ""
	for i=1, #txt2 do
		txt = txt .. makeEight( table.concat( toBits( string.byte( txt2:sub( i, i ) ) ) ):reverse() )
	end
	return txt
end

function binaryToText( txt )
	local txt2 = ""
	local t = txt:split( "\n" )
	for k, v in pairs( t ) do
		q = 1
		for i=1, #v / 8 do
			txt2 = txt2 .. string.char( tonumber( v:sub( q, q + 7), 2 ) )
			q = q + 8
		end
	end
	return txt2
end
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

This doesn't work…
DannySMc #2
Posted 23 March 2015 - 03:21 PM
code
--lua text => binary ascii codes

function string:split(delimiter)
	local result = { }
	local from = 1
	local delim_from, delim_to = string.find( self, delimiter, from )
	while delim_from do
		table.insert( result, string.sub( self, from , delim_from-1 ) )
		from = delim_to + 1
		delim_from, delim_to = string.find( self, delimiter, from )
	end
	table.insert( result, string.sub( self, from  ) )
	return result
end

function toBits(num)
	local t={}
	while num>0 do
		rest=math.fmod(num,2)
		t[#t+1]=rest
		num=(num-rest)/2
	end
	return t
end

function makeEight( t )
	return ("0"):rep( 8 - #t ) .. t
end

function textToBinary( txt2 )
	local txt = ""
	for i=1, #txt2 do
		txt = txt .. makeEight( table.concat( toBits( string.byte( txt2:sub( i, i ) ) ) ):reverse() )
	end
	return txt
end

function binaryToText( txt )
	local txt2 = ""
	local t = txt:split( "\n" )
	for k, v in pairs( t ) do
		q = 1
		for i=1, #v / 8 do
			txt2 = txt2 .. string.char( tonumber( v:sub( q, q + 7), 2 ) )
			q = q + 8
		end
	end
	return txt2
end
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

This doesn't work…

Why has this been moved to ask a pro? I just pointed out it didn't work? -.-

Well now it is here, can anyone fix that?:D/>
Lyqyd #3
Posted 23 March 2015 - 03:28 PM
It wasn't moved. You necro'd an ancient Ask a Pro topic with what would be a useless post if you weren't looking for help with that code. I gave you the benefit of the doubt and split the post into its own new topic so you can get a more full answer. For anyone curious about the content of the original topic, follow the link in the quote in the first post here, the arrow just left of, "Alice, on …".
DannySMc #4
Posted 23 March 2015 - 03:58 PM
Fair enough, well if anyone can actually fix this… I want to get a few functions that will convert strings/numbers to binary and then back again…?
Dragon53535 #5
Posted 23 March 2015 - 07:30 PM
*cough* Made it months ago for strings. *cough* Also, you can use the numberString function to turn numbers TO binary. and the built in tonumber function to convert numbers from binary, as such:


numberString(100) --> 1100100
toBinary("b") --> 01100100 (b is technically the char 100)
fromBinary("01100100") --> b
tonumber(1100100,2) --> 100 (from base 2, you need that ,2 at the end so that it knows
Edited on 23 March 2015 - 08:36 PM