Indigo Rose Software

Professional Software Development Tools

 
Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2003
    Location
    West Monroe, LA
    Posts
    294

    Dec -> Bin formula

    Does anyone know a formula that I can use to convert Dec to Bin.
    I want to make an app that will get an IP address from the user and convert it to the binary format.

  2. #2
    Join Date
    Sep 2002
    Location
    Sol 3
    Posts
    3,160
    Try this. This will convert a number between 0 and 255 to binary. It is probably not fool proof and there is probably a better way to do it but give this a shot.

    Code:
    function Dec2Bin(nInteger)
    	local sBinary = "";
    	for x = 8, 0, -1 do
    		if nInteger - Math.Pow(2,x)< 0 then 
    			sBinary = sBinary .. "0";
    		else
    			sBinary = sBinary .. "1";
    			nInteger = nInteger - Math.Pow(2,x);
    		end
    	end
    	return sBinary;
    end
    I am sure there is a way to calculate what the max value of x would be and ensure that the number is calculated correctly so that nInteger ends up at 0.

    Tigg
    TJ-Tigger
    "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
    "Draco dormiens nunquam titillandus."
    Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

  3. #3
    Join Date
    Oct 2003
    Location
    West Monroe, LA
    Posts
    294
    You made it look so easy...

    Thanks alot!!

  4. #4
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959

    So many ways to skin a cat

    My version...

    Code:
    function Dec2Bin(nDecimal)
        nBin = Math.Mod(nDecimal,2)
        nDecimal = Math.Floor(nDecimal / 2)
    
       while nDecimal ~= 0 do
            nBin = Math.Mod(nDecimal,2) .. nBin
            nDecimal = Math.Floor(nDecimal / 2)
       end
    
        return nBin
    end
    
    Dialog.Message("", Dec2Bin(5845223))
    reference link:
    http://www.vb-helper.com/howto_decimal_to_binary.html

  5. #5
    Join Date
    Sep 2002
    Location
    Sol 3
    Posts
    3,160
    Here I go making things more difficult. I realized that LUA does not support math.log where you can specify the base. In this case I was trying to figure out how to do log2 and use that to find the e for the specified number. So here is a revised complicated way to do the same thing. aaaarrrrrgggghh

    Code:
    function log2(n)
    local _n = 2
    local x = 1
        if (_n < n) then
            repeat
                x = x + 1
                _n = _n + _n
            until (_n >= n)
        elseif (_n > n) then
            if (n == 1) then
                return 0
            else
                return nil
            end
        end 
        if (_n > n) then
            return x-1
        else
            return x
        end 
    end 
    
    function Dec2Bin(nInteger)
    	local sBinary = "";
    	Dialog.Message("log", log2(nInteger));
    	if log2(nInteger) < 7 then
    		nMaxPower = 7;
    	else 
    		nMaxPower = Math.Floor(log2(nInteger));
    	end
    	--Dialog.Message("log", nMaxPower);
    	for x = nMaxPower, 0, -1 do
    		if nInteger - Math.Pow(2,x)< 0 then 
    			sBinary = sBinary .. "0";
    		else
    			sBinary = sBinary .. "1";
    			nInteger = nInteger - Math.Pow(2,x);
    		end
    	end
    	return sBinary;
    end
    
    sBinary = Dec2Bin(255)
    Tigg
    TJ-Tigger
    "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
    "Draco dormiens nunquam titillandus."
    Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

  6. #6
    Join Date
    Sep 2002
    Location
    Sol 3
    Posts
    3,160
    and even simpler. to find a logarithm of a specific power for instance the power of 2 (log2) use the following


    log2 x = math.log(x)/math.log(2)
    log3 x = math.log(x)/math.log(3)
    Code:
    function Dec2Bin(nInteger)
    	local sBinary = "";
    	--Dialog.Message("log", log2(nInteger));
    	if math.log(nInteger)/math.log(2) < 7 then
    		nMaxPower = 7;
    	else 
    		nMaxPower = Math.Floor(math.log(nInteger)/math.log(2));
    	end
    	--Dialog.Message("log", nMaxPower);
    	for x = nMaxPower, 0, -1 do
    		if nInteger - Math.Pow(2,x)< 0 then 
    			sBinary = sBinary .. "0";
    		else
    			sBinary = sBinary .. "1";
    			nInteger = nInteger - Math.Pow(2,x);
    		end
    	end
    	return sBinary;
    end
    
    sBinary = Dec2Bin(255)
    It has been toooooooo long since i have had to think with that kind of math
    TJ-Tigger
    "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
    "Draco dormiens nunquam titillandus."
    Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

  7. #7
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    Geez Tig, we're gonna run out of cats if you keep it up

  8. #8
    Join Date
    Sep 2002
    Location
    Sol 3
    Posts
    3,160
    Yeah, I don't think I rate for OCD but sometimes when I get a bee in my bonnet I like to get it out and I was not satisfied of my previoius skinning attempts. I figured there had to be a better way.

    Also, I was wondering why there were not more Logarithim functions in LUA. Often there is LN, LOG and LOG10. The lua function math.log or AMS Math.Log is the same as ln which is a natural log, base of e and math.log10 where the base by default is 10. None of the allowed you to specify the base so I just wanted to figure it out.

    Maybe I am OC. Who knows.

    but I learned something new. . . now if I can just put it to use again.

    Tigg
    TJ-Tigger
    "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
    "Draco dormiens nunquam titillandus."
    Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

  9. #9
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    I'm having flashbacks of when you schooled me on tonumber

  10. #10
    Join Date
    Sep 2002
    Location
    Sol 3
    Posts
    3,160
    Sometimes one has to share cool stuff when it is found.

    that and string.format I have found to be most useful

    Tigg
    TJ-Tigger
    "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
    "Draco dormiens nunquam titillandus."
    Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

  11. #11
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    It's cool to be schooled

  12. #12
    Join Date
    Sep 2002
    Location
    Sol 3
    Posts
    3,160
    Yes, yes it is. I find that I am schooled by your posts all the time.
    TJ-Tigger
    "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
    "Draco dormiens nunquam titillandus."
    Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

  13. #13
    Join Date
    Dec 2003
    Location
    Location! Location!
    Posts
    6,137
    Hear hear... I've been schooled by both of you a couple/few times and yet I still have yet to receive my diploma!

    Ah? Diploma please!

    Intrigued

  14. #14
    Join Date
    Sep 2002
    Location
    Sol 3
    Posts
    3,160
    Con-GRAD-ulations
    TJ-Tigger
    "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
    "Draco dormiens nunquam titillandus."
    Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

Similar Threads

  1. Could someone explain FILE > EXECUTE > working directory
    By Corey in forum AutoPlay Media Studio 4.0
    Replies: 1
    Last Post: 12-12-2002, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts