Posts Tagged ‘ActionScript

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