View Full Version : decimal number to binary number ?

05-20-2003, 09:01 AM
In ActionScript, I can use parseInt to convert binary num to decimal number. But how can I convert decimal number to binary number? I try find in AS dictionary but there isn't result.
I have an input textfield to enter the decimal number, and an other to display the binary number.
Please tell me how...Thx U,

05-20-2003, 09:07 AM

Try this???


05-20-2003, 09:23 AM
pixelwit did one before, was great but I didn't save the URL and I searched alot but I didn't :(
u can look for it with me

05-20-2003, 10:19 AM
Thx for Ur help, I will try it.
I searched in the Forum, but no thread about it excluded my thread :D

05-20-2003, 10:38 AM
I know that, think he posted it withen another post not as a seperate one
think we should PM him for that

03-26-2004, 10:41 PM
Here is a quick and crude method to convert a decimal number to a binary string in Flash MX:

// convert decimal number to binary string
function dec2bin(iNumber) {
var bin = "";
var oNumber = iNumber;
while (iNumber>0) {
if (iNumber%2) {
bin = "1"+bin;
} else {
bin = "0"+bin;
iNumber = Math.floor(iNumber/2);
// left pad with zeros
while (bin.length<15) {
bin = "0"+bin;
return bin;

Found this thread while searching for a better version than what is posted above. :)

03-26-2004, 11:45 PM
as tags not working ... still not working :(

03-26-2004, 11:48 PM
here's one, but I just came up with it so it could very easily be very wrong ;) I did some tests, though, and it seemed to be accurate.

seems to be right because its about the same as yours, jtarbell, just a bit slimmer

-splictfunction dec2bin(num) {
var bin = "";
while (num) {
bin = num % 2 + bin;
num = Math.floor(num / 2);
return (bin) ? bin : 0;

03-28-2004, 09:10 PM
createTextField("Decimal", 10, 25, 25, 525, 20);
Decimal.text = "ten";
Decimal.type = "input";
createTextField("Binary", 20, 25, 75, 525, 20);
Binary.text = "two";
Binary.wordWrap = true;
Decimal.onChanged = function(){
var numbr = parseInt(this.text);
Binary.text = numbr.toString(2);

03-28-2004, 09:42 PM
Number.toString - thats a new one for me! Thanks Pixelwit. :) Silly that I didn't know that.

Still, I enjoyed figuring that out. I'm trying to learn some math again. I wish we had all these math websites back when I was in Geometry and Trig in High school. :rolleyes:


03-28-2004, 10:20 PM


03-29-2004, 04:12 AM
pixelwit did one before, was great but I didn't save the URL and I searched alot but I didn't :(
u can look for it with me
Now here is the man himself