Hi I would like to know if you can convert decimal to binary using actionscript 3.0?

Copy link to clipboard
Copied
Hi I would like to know if you can convert decimal to binary using actionscript 3.0?
Copy link to clipboard
Copied
use:
function decimalToBinary(n:Number):String{
return n.toString(2);
}

Copy link to clipboard
Copied
Hi Ive used this code but its not worked what I am trying to do is input a decimal value into an input textbox to enter this and return the binary conversion value in another input textbox. Please let me know if you can still help.
Copy link to clipboard
Copied
there are no issues as long as your treat the binary as a string, not a number.

Copy link to clipboard
Copied
Hi thanks for the response Ive tried to implement the code as you stated but it is not outputting please if you could check the following code and let me know where Ive gone wrong that would be much appreciated Thanks!.
miles_txt.addEventListener(KeyboardEvent.KEY_DOWN,convertKilometres);
miles_txt.restrict ="0-9";
function convertKilometres (k:KeyboardEvent):void {
var miles:Number;
var kilometres:Number;
if (k.keyCode == Keyboard.ENTER){
miles=Number(miles_txt.text);
kilometres=miles* 1.609344;
kilometres_txt.text = kilometres.toPrecision(2);
}
}
kilometreConvert_txt.addEventListener(KeyboardEvent.KEY_DOWN,convertKiloToMile);
kilometreConvert_txt.restrict = "0-9";
function convertKiloToMile(k:KeyboardEvent):void{
var kilometreConvert:Number;
var milesAnswer:Number;
if (k.keyCode==Keyboard.ENTER) {
kilometreConvert=Number(kilometreConvert_txt.text);
milesAnswer=kilometreConvert* 0.621371192;
milesAnswer_txt.text = milesAnswer.toPrecision(2);
}
}
binary_txt.addEventListener(KeyboardEvent.KEY_DOWN, checkEnterKey2);
function checkEnterKey2(e:KeyboardEvent):void{
if(binary_txt.text != '' && e.keyCode == Keyboard.ENTER){
decimal_txt.text=binaryToDecimal(binary_txt.text).toString();
}
}
function binaryToDecimal(s:String):Number{
var n:Number = 0
for(var i:int=0;i<s.length;i++){
n+=Number(s.substr(i,1))<<(s.length-1-i)
}
return n;
}
function decimalToBinary(n:Number):String{
return n.toString(2);
}
return_btn.addEventListener(MouseEvent.CLICK, goBackToCalculator);
function goBackToCalculator(e:Event):void
{
mybuttonSound.play();
gotoAndStop("Calculator");
}
clear_btn.addEventListener(MouseEvent.CLICK,clearField);
function clearField(e:MouseEvent):void{
mybuttonSound.play();
miles_txt.text ="";
kilometres_txt.text ="";
milesAnswer_txt.text ="";
kilometreConvert_txt.text ="";
binary_txt.text ="";
decimal_txt.text ="";
}
Copy link to clipboard
Copied
i dont' see where you're calling decimalToBinary().

Copy link to clipboard
Copied
Hi Kglad,
Thanks for your support in this query but I decided to change this conversion as Ive implemented another conversion code into my application using AS 3.0. Thanks for your support again and sorry for any inconvience caused as I will use this forum shortly again for any help needed.
Thanks Again
AIQBAL777

