May 14, 2010

AS3 DECIMAL TO BINARY, OCTAL AND HEXADECIMAL CONVERTER

This converts decimal into binary, octal and hexadecimal. I have no idea why I made this. In fact, there's probably a native function that does this ten times quicker. It was fun, though. Call it an adventure into user input.

The real work is in converting decimal to binary. To do this, you have to repeatedly divide by two (the base). In doing this, you'll get either a "1" or a "0" as a remainder. You can use the modulus operator to do this, but I just divided by two and later checked to see if the floor of the number was equal to the original number. If so, then there was no remainder, and I added a "0". Otherwise I added a "1".

Going from binary to octal and hex is very easy. You just need to split the binary string into lengths of three and map this new value into binary. For instance, the string "101111001" can be split into three units: 101, 111, and 001. These, in turn map to the binary values of "5", "7", and "1". So, 101111001 base 2 (binary) equals 571 base 8 (octal).

Binary to hex is just as easy, only you have to split the string into units with lengths of 4. So, 11110010 is split into "1111" and "0010", which in hex is "F2".

One problem that I ran into during this project was the use of the TextField's TEXT_INPUT TextEvent. It sucks, and unless I'm missing something I see no real reason to use it. It lags by one character, so that if you type in "hello world", your app thinks that "hello worl" has been entered. It shortens the incoming string by one character. I found that getting the TextField's data with a "KEY_UP" Keyboard event is more accurate. The only drawback is that you either have to constantly listen for the KEY_UP event, or you do as I did here: start listening when the TextField's FOCUS_IN event fires and stop listening on FOCUS_OUT (this uses AS3's FocusEvent. There are probably other solutions, but this worked great for me.

Download The SWF and FLA Here:

Here's the plain text source code:

import flash.text.*;
import flash.ui.*;

var gf:GlowFilter = new GlowFilter(0x333333, 0.6, 5, 5);
var binaryArray:Array = new Array();
var num:Number;
var binaryString:String;
var round:Number;
var bString:String;
var bNumber:Number;
var tripletArray:Array = new Array();
var extraBits:int;
var leadingZeros:String;
var octalString:String = "";
var len:int = 0;
var hexArray:Array = new Array();
var hexString:String = "";


// please ignore laziness below (these should be in a for loop)

//___________________________________________________________________text_fields____
// common format
var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = 0x000000;
format.size = 12;

// label for decimal number field
var tf2:TextField = new TextField();
tf2.height = 18;
tf2.width = 200;
tf2.defaultTextFormat = format;
tf2.x = 25;
tf2.y = 25;
tf2.text = "Decimal Number:";
tf2.selectable = false;
addChild(tf2);

// decimal number input field
var tf:TextField = new TextField();
tf.type = TextFieldType.INPUT;
tf.background = true;
tf.border = true;
tf.height = 18;
tf.width = 300;
tf.x = 25;
tf.y = 50;
tf.restrict = "0-9";
tf.defaultTextFormat = format;
addChild(tf);
tf.addEventListener(FocusEvent.FOCUS_IN, startFocus);
tf.addEventListener(FocusEvent.FOCUS_OUT, endFocus);


// label for binary
var tf4:TextField = new TextField();
tf4.height = 18;
tf4.width = 200;
tf4.defaultTextFormat = format;
tf4.x = 25;
tf4.y = 100;
tf4.text = "Binary Number:";
tf4.selectable = false;
addChild(tf4);

// display text field for results (binary)
var tf3:TextField = new TextField();
tf3.type = TextFieldType.DYNAMIC;
tf3.background = true;
tf3.backgroundColor = 0xE5E5E5;
tf3.border = true;
tf3.height = 18;
tf3.width = 300;
tf3.x = 25;
tf3.y = 125;
tf3.defaultTextFormat = format;
addChild(tf3);


// label for octal
var tf6:TextField = new TextField();
tf6.height = 18;
tf6.width = 200;
tf6.defaultTextFormat = format;
tf6.x = 25;
tf6.y = 175;
tf6.text = "Octal Number:";
tf6.selectable = false;
addChild(tf6);

// display text field for results (octal)
var tf5:TextField = new TextField();
tf5.type = TextFieldType.DYNAMIC;
tf5.background = true;
tf5.backgroundColor = 0xE5E5E5;
tf5.border = true;
tf5.height = 18;
tf5.width = 300;
tf5.x = 25;
tf5.y = 200;
tf5.defaultTextFormat = format;
addChild(tf5);

// label for hex
var tf8:TextField = new TextField();
tf8.height = 18;
tf8.width = 200;
tf8.defaultTextFormat = format;
tf8.x = 25;
tf8.y = 250;
tf8.text = "Hexadecimal Number:";
tf8.selectable = false;
addChild(tf8);

// display text field for results (hex)
var tf7:TextField = new TextField();
tf7.type = TextFieldType.DYNAMIC;
tf7.background = true;
tf7.backgroundColor = 0xE5E5E5;
tf7.border = true;
tf7.height = 18;
tf7.width = 300;
tf7.x = 25;
tf7.y = 275;
tf7.defaultTextFormat = format;
addChild(tf7);





//__________________________________________________________________________________
function checkEnterKey(e:KeyboardEvent):void
{
	if(tf.text != '' && e.keyCode == Keyboard.ENTER)
	{
		num = Number(tf.text);
		//trace(e.keyCode + " : " + tf.text);
		bString = convertDecimalToBinary(num);
		bNumber = Number(binaryString);
		tf3.text = bString;
		
		tf5.text = convertBinaryToOctal(bString);
		
		tf7.text = convertBinaryToHex(bString);
	}
}
//__________________________________________________________________________________
function getTextInput(e:KeyboardEvent):void
{
	num = Number(tf.text);
	//trace(e.keyCode + " : " + tf.text);
	bString = convertDecimalToBinary(num);
	bNumber = Number(binaryString);
	tf3.text = bString;
	
	tf5.text = convertBinaryToOctal(bString);
	
	tf7.text = convertBinaryToHex(bString);
}
//__________________________________________________________________________________
function convertDecimalToBinary(num:Number):String
{
	if(num == 0)
		return String("0");
		
	binaryString = '';	//start with empty string each time
	
	while(num > 0)
	{
		num /= 2;
		
		if(Math.floor(num) == num)
			binaryArray.push(0);
		else
			binaryArray.push(1);
		
		trace(num);
		num = Math.floor(num);
	}
	
	for(var i:int = binaryArray.length - 1; i >= 0; i--)
	{
		binaryString += binaryArray[i];
		// clear array to prepare for next input
		binaryArray.pop();
	}
	
	//trace(binaryString);
	return binaryString;
}



//__________________________________________________________________________________
function convertBinaryToOctal(bStr:String):String
{
	octalString = '';
	
	// split binary string into units of three
	for(var i:int = bStr.length; i > 0; i -= 3)
	{
		tripletArray.push(bStr.substring(i - 3, i));
	}
	
	len = tripletArray.length - 1;
	
	// add leading zeros to last array item
	extraBits = tripletArray[len].length;
	if(extraBits == 1)
	{
		tripletArray[len] = "00" + tripletArray[len];
		//tripletArray[tripletArray.length - 1] = leadingZeros;
	}
	else if(extraBits == 2)
	{
		tripletArray[len] = "0" + tripletArray[len];
		//tripletArray[tripletArray.length - 1] = leadingZeros;
	}
		
	for(var j:int = 0; j < len + 1; j++)
		trace(tripletArray[j]);
	
	// switch octal to binary
	for(var l:int = len; l >= 0; l--)
	{
		switch(tripletArray[l])
		{
			case "000":
				tripletArray[l] = "0";
				break;
			case "001":
				tripletArray[l] = "1";
				break;
			case "010":
				tripletArray[l] = "2";
				break;
			case "011":
				tripletArray[l] = "3";
				break;
			case "100":
				tripletArray[l] = "4";
				break;
			case "101":
				tripletArray[l] = "5";
				break;
			case "110":
				tripletArray[l] = "6";
				break;
			case "111":
				tripletArray[l] = "7";
				break;
			default:
				break;
		}
		octalString += tripletArray[l];
		tripletArray.pop();
	}
	
	return octalString;
}

//__________________________________________________________________________________
function convertBinaryToHex(bStr:String):String
{
	trace("bstring = " + bStr);
	
	hexString = '';
	
	// split binary string into units of three
	for(var i:int = bStr.length; i > 0; i -= 4)
	{
		hexArray.push(bStr.substring(i - 4, i));
	}
	
	len = hexArray.length - 1;
	
	// add leading zeros to last array item
	extraBits = hexArray[len].length;
	if(extraBits == 1)
	{
		hexArray[len] = "000" + hexArray[len];
	}
	else if(extraBits == 2)
	{
		hexArray[len] = "00" + hexArray[len];
	}
	else if(extraBits == 3)
	{
		hexArray[len] = "0" + hexArray[len];
	}

	// switch binary to hex
	for(var l:int = len; l >= 0; l--)
	{
		switch(hexArray[l])
		{
			case "0000":
				hexArray[l] = "0";
				break;
			case "0001":
				hexArray[l] = "1";
				break;
			case "0010":
				hexArray[l] = "2";
				break;
			case "0011":
				hexArray[l] = "3";
				break;
			case "0100":
				hexArray[l] = "4";
				break;
			case "0101":
				hexArray[l] = "5";
				break;
			case "0110":
				hexArray[l] = "6";
				break;
			case "0111":
				hexArray[l] = "7";
				break;
			case "1000":
				hexArray[l] = "8";
				break;
			case "1001":
				hexArray[l] = "9";
				break;
			case "1010":
				hexArray[l] = "A";
				break;
			case "1011":
				hexArray[l] = "B";
				break;
			case "1100":
				hexArray[l] = "C";
				break;
			case "1101":
				hexArray[l] = "D";
				break;
			case "1110":
				hexArray[l] = "E";
				break;
			case "1111":
				hexArray[l] = "F";
				break;
			default:
				break;
		}
		hexString += hexArray[l];
		hexArray.pop();
	}
	
	return hexString;
}


//__________________________________________________________________________________
function startFocus(e:FocusEvent):void
{
	tf.filters = [gf];
	stage.addEventListener(KeyboardEvent.KEY_UP, getTextInput);
}

//__________________________________________________________________________________
function endFocus(e:FocusEvent):void
{
	tf.filters = [];
	stage.removeEventListener(KeyboardEvent.KEY_UP, getTextInput);
}

Posted in actionscript

Comments:


hey, why you did such efforts? why you didn't use the Number.toString(2), Number.toString(8),  Number.toString(16) functions? its built-in in AS3. :P

Mudasir | October 26, 2011 @ 6:15 am

or parseInt("100001", 2)

kong | December 2, 2011 @ 9:09 am

Immensely helpful... i could now see exactly what bytes and where they cam from by comparing in the hex editor...Thanks

Chris Ellem | February 1, 2012 @ 4:21 am

What do you think?

(All fields required. Only your name and comment will be stored.)
Name:

Email:

Code:
validation image
Message: