Tutorials Books Videos Forums

-- online Change the theme! Search!
Rambo ftw!

Customize Theme


Color

Background


Done
Zelwyn

Binary-Decimal Conversion

by Zelwyn   | filed under Flash and ActionScript

In this tutorial, I'm going to introduce you to the binary number system, if you don't already know it, and teach you to convert a decimal number into binary and vice versa using a little bit of code in Flash.

First of all, I assume all of you know what the decimal number system is. It's the one we use everyday! Its base number is ten, meaning that when you get past nine in one place, you increase the next place value by one. Each place value is equal to the one before it multiplied by ten. Binary is a little different.

When we talk about binary, we are talking about the "1's and 0's" language you think about if you're a big computer geek. Your computer is made up of a whole lot of switches, each triggering an action in your computer when flipped. A 1 in binary symbolizes an "on" in the computer, whereas a 0 symbolizes an "off". The binary number system has a base number of two, meaning that each place is equal to the one before it times two.

So that being said, if the first digit on the far right is 1, then the place values go in this order from right to left:

256, 128, 64, 32, 16, 8, 4, 2, 1

[ an example of the order of places ]

Converting Normally

Here I'll teach you how to look at any binary number and convert it into decimal.

010011110

This number is equal to 158. How did I get that? Well, if you follow the place values that I showed you in the beginning, the first digit is 1, followed by 2, and so on. Each place where there is a one, you add the number in that place to your total. Since there's a 0 in the "1's" place, we don't add 1. Since there's a 1 in the "2's" place, we add 2.

There's a 1 in each of these places: 2, 4, 8, 16, and 128, so 2 + 4 + 8 + 16 + 128 = 158. That's so easy isn't it? Here, try this one:

101010101

By the way, when doing this while at the computer, the scientific mode of Windows Calculator has a feature to convert between hexadecimal, decimal, octal, and binary. You should have gotten 341 if you did it right.

To convert from decimal is much harder. You take the next lowest place value from your number, unless your number is the same as a place value such as 32, and you divide your number by that place. Take the remainder, rinse, and repeat until you get to 1. So, if our number was 100, we would divide it by 64 and get 36. We would then divide that by 32 and get 4. We divide 4 by 4 and get 1. Then we make our number and put a 1 in every place that we divided by. So, 100 in binary is 1100100.

Converting in Flash

Here I'll teach you how to convert any binary number to decimal and vice versa in Flash.

To convert from binary to decimal, just like in real life, is much easier to do in Flash than the other way around. Add this code into the first frame of your movie:

// initialize the place values
place = [32, 16, 8, 4, 2, 1];

// initialize your binary number
binary = [1, 1, 0, 0, 1, 1];

// trace it as a string
trace(binary.join(""));

// convert it to a decimal number
decimalNumber = (place[0] * binary[0]) +
                (place[1] * binary[1]) +
                (place[2] * binary[2]) +
                (place[3] * binary[3]) +
                (place[4] * binary[4]) +
                (place[5] * binary[5]);

// trace the result
trace(decimalNumber);

What this does is it makes two arrays that correspond the binary number to the place values that it goes with. It then multiplies each value by the value in the binary array, and adds them together. You could make a text field display them.

Now add this code for the conversion the other way around:

// convert decimal number to binary string
function dec2bin(iNumber) {
  var bin = "";
  var oNumber = iNumber;

  // this while method constructs a string from the number
  // you enter as iNumber when you call the function
  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;
}

What this does is adds a 1 and 0 for each place where they're needed. If you uncomment the while method in there, it'll add zeroes until it's a certain length, determined by the "15" in that while statement. That should be good for conversion of numbers. I might make one on octal or hexadecimal eventually.

For any of you that want to learn about other number systems I would suggest a book called How to Count Like a Martian by Glory St. John, and Cool Math by Christy Maganzini and Ruta Daugavietis. They helped me a lot.

Anyone interested in binary might also want to check out my Binary Clock. If you can't read it, go to the version with numbers. You can probably expect me to make tutorials on other systems sooner or later. Cya!

Peace be with you...

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums.

Your support keeps this site going!

- Zelwyn

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--