27
Mar
09

[ActionScript] Integer To Binary Algorithm

Ahhha so, since ive been studying a few algorithms recently, I thought I would code one to test it out. This uses a basic loop to convert a number to binary:

function integerToBinary(number:Number):String {
	var binary:String = "";
	while (number != 0) {
		if (number%2 == 0) {
			binary = "0"+binary;
		} else {
			binary = "1"+binary;
			number--;
		}
		number = number/2;
	}
	return binary;
}

Usage:

trace(_root.integerToBinary(123));

Would output:

1111011

Advertisements

0 Responses to “[ActionScript] Integer To Binary Algorithm”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: