Announcement

Collapse
No announcement yet.

Working with bits and bytes

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Jules Graus
    replied
    Done

    Made the following functions:
    ToBinary(decimal, byte_separating_dots)
    ToDecimal(binary)
    BinDecOperation(numberone, typeone, numbertwo, typetwo, operator, resulttype, binary_dots)
    String.Reverse(data)

    Code:
    function ToBinary(decimal, byte_separating_dots) --decimal to 32 bits
    	decimal = String.ToNumber(""..decimal.."")
    	if decimal > 4294967295 then
    		Dialog.Message("Error", "The number may not be bigger then 4294967295.", MB_OK, MB_ICONNONE, MB_DEFBUTTON1)
    	else
    		local decimal_working = decimal 
    		local binary = ""
    		local try = ""
    
    		local curr_bit = 32
    		
    		while curr_bit > 0 do
    			if byte_separating_dots == true then
    				if curr_bit == 24 or curr_bit == 16 or curr_bit == 8 then
    					binary = binary.."."
    				end
    			end
    		
    			
    			try = Math.Pow(2, curr_bit-1)
    			if (decimal_working - try) >= 0 then
    				decimal_working = decimal_working - try
    				binary = binary.."1"
    			else
    				binary = binary.."0"
    			end
    			
    			curr_bit = curr_bit - 1	
    		end
    		
    		return binary
    	end
    end
    
    function ToDecimal(binary) --binary (max 32 bits) to decimal
    	binary = String.Replace(binary, ".", "", false)
    	
    	local length = String.Length(binary)
    	if length > 32 then
    		Dialog.Message("Fout", "The number may, but not exceed 32 bits.", MB_OK, MB_ICONNONE, MB_DEFBUTTON1)
    	else
    		local decimal = 0
    		local add = 0
    		
    		for x = length, 1, -1 do
    			char = String.Mid(""..String.Reverse(binary).."", x, 1)
    			
    			if char == "1" then
    				add = Math.Pow(2, x-1)
    			elseif char == "0" then
    				add = 0
    			end
    			
    			decimal = decimal + add
    		end
    		
    		return decimal
    	end
    end
    
    function BinDecOperation(numberone, typeone, numbertwo, typetwo, operator, resulttype, binary_dots) --Does an operation on binary and decimal numbers. Example: result = BinDecOperation("12", "Decimal", "1010", "+", "Decimaal", false). Adds 1010 (decimal 10) to decimal number 12. Decimal result: 22. If you add an optional boolean you can specify whether you want (true) or dont want (false) to add dots every 8 bits.
    	if operator == "+" or operator == "-" or operator == "/" or operator == "*" or operator == "%" or operator == "^" then
    		if typeone == "Decimal" or typeone == "Binary" then
    			if typetwo == "Decimal" or typetwo == "Binary" then
    				if resulttype == "Decimal" or resulttype == "Binary" then
    					local result
    					
    					if typeone == "Binary" then
    						numberone = ToDecimal(numberone)
    					end
    					
    					if typetwo == "Binary" then
    						numbertwo = ToDecimal(numbertwo)
    					end
    					
    					if operator == "+" then
    						result = numberone + numbertwo
    					elseif operator == "-" then
    						result = numberone - numbertwo
    					elseif operator == "/" then
    						result = numberone / numbertwo
    					elseif operator == "*" then
    						result = numberone * numbertwo
    					elseif operator == "%" then
    						result = numberone % numbertwo
    					elseif operator == "^" then
    						result = numberone ^ numbertwo
    					end
    					
    					if resulttype == "Binary" then
    						return ToBinary(result, binary_dots)
    					elseif resulttype == "Decimal" then
    						return result
    					end
    				else
    					Dialog.Message("Error", "resulttype must be Decimal or Binary")
    				end
    			else
    				Dialog.Message("Error", "typetwo must be Decimal or Binary")
    			end
    		else
    			Dialog.Message("Error", "typeone must be Decimal or Binary")
    		end
    	else
    		Dialog.Message("Error", "You didn't specify a valid operator. Valid operators are: + - // * % ^. You need to specify them in a string")
    	end
    end
    
    function String.Reverse(data)
    	local Length = String.Length(data)
    	local result = ""
    	local char = ""
    	
    	for x = Length, 1, -1 do
    		char = String.Mid(data, x, 1)
    		result = result..char
    	end
    	
    	return result
    end
    Last edited by Jules Graus; 10-17-2013, 12:23 PM. Reason: Forgot code tags and explanation

    Leave a comment:


  • Jules Graus
    replied
    Thank you.

    Thank you that solves multiplication at least.

    I've made a couple of functions already. They convert between decimal and binary and vice versa.
    Now i need to solve the addition and subtraction of binary numbers. Shouldnt take to long . And i also need to improve the ToBinary function a bit.

    Code:
    function ToBinary(decimal, byte_separating_dots) --decimal to 32 bits
    	decimal = String.ToNumber(""..decimal.."")
    	local decimal_working = decimal 
    	local binary = ""
    	local try = ""
    
    	local curr_bit = 32
    	
    	while curr_bit > 0 do
    		if byte_separating_dots == true then
    			if curr_bit == 24 or curr_bit == 16 or curr_bit == 8 then
    				binary = binary.."."
    			end
    		end
    	
    		
    		try = Math.Pow(2, curr_bit-1)
    		if (decimal_working - try) >= 0 then
    			decimal_working = decimal_working - try
    			binary = binary.."1"
    		else
    			binary = binary.."0"
    		end
    		
    		curr_bit = curr_bit - 1	
    	end
    	
    	return binary
    end
    
    function ToDecimal(binary) --binary (max 32 bits) naar decimaal
    	binary = String.Replace(binary, ".", "", false)
    	
    	local length = String.Length(binary)
    	if length > 32 then
    		Dialog.Message("Error", "The number should not exceed 32 bits.", MB_OK, MB_ICONNONE, MB_DEFBUTTON1)
    	else
    		local decimal = 0
    		local add = 0
    		
    		for x = length, 1, -1 do
    			char = String.Mid(""..String.Reverse(binary).."", x, 1)
    			
    			if char == "1" then
    				add = Math.Pow(2, x-1)
    			elseif char == "0" then
    				add = 0
    			end
    			
    			decimal = decimal + add
    		end
    		
    		return decimal
    	end
    end
    
    function String.Reverse(data)
    	local Length = String.Length(data)
    	local result = ""
    	local char = ""
    	
    	for x = Length, 1, -1 do
    		char = String.Mid(data, x, 1)
    		result = result..char
    	end
    	
    	return result
    end

    Leave a comment:


  • Ulrich
    replied
    This may possibly help.

    Ulrich

    Leave a comment:


  • Jules Graus
    started a topic Working with bits and bytes

    Working with bits and bytes

    Hello people,

    I need to do binary operations. at least be able to add and subtract binairy numbers from eachother. And if possible i want to convert between binary and decimal. Does lua have some functions to do this?

    I want to create a IPv4 Subnetting and VLSM CIDR tool.

    Thanks in advance
Working...
X
😀
🥰
🤢
😎
😡
👍
👎