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.
Professional Software Development Tools
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.
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.
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.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
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
You made it look so easy...
Thanks alot!!
My version...
reference link: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))
http://www.vb-helper.com/howto_decimal_to_binary.html
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
TiggCode: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)
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
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)
It has been toooooooo long since i have had to think with that kind of mathCode: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)![]()
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
Geez Tig, we're gonna run out of cats if you keep it up![]()
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
I'm having flashbacks of when you schooled me on tonumber![]()
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
It's cool to be schooled
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
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
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