View Full Version : Dec -> Bin formula
markstaylor
08-09-2005, 05:35 PM
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.
TJ_Tigger
08-09-2005, 09:21 PM
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.
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
markstaylor
08-09-2005, 09:26 PM
You made it look so easy...
Thanks alot!!
My version...
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
TJ_Tigger
08-09-2005, 10:30 PM
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
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
08-10-2005, 12:20 PM
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)
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 :eek:
Geez Tig, we're gonna run out of cats if you keep it up :)
TJ_Tigger
08-10-2005, 12:34 PM
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
I'm having flashbacks of when you schooled me on tonumber :yes
TJ_Tigger
08-10-2005, 12:44 PM
Sometimes one has to share cool stuff when it is found. :D :D
that and string.format I have found to be most useful
Tigg
TJ_Tigger
08-10-2005, 01:43 PM
Yes, yes it is. I find that I am schooled by your posts all the time. :)
Intrigued
08-10-2005, 01:52 PM
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!
:D
TJ_Tigger
08-10-2005, 02:17 PM
Con-GRAD-ulations
http://s94928149.onlinehome.us/pub/diploma.jpg
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.