Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Guest
Mar 19, 2012 Mar 19, 2012

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

TOPICS
ActionScript
889
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 19, 2012 Mar 19, 2012

use:

function decimalToBinary(n:Number):String{

return n.toString(2);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 20, 2012 Mar 20, 2012

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 20, 2012 Mar 20, 2012

there are no issues as long as your treat the binary as a string, not a number.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 20, 2012 Mar 20, 2012

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 ="";
 
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 20, 2012 Mar 20, 2012

i dont' see where you're calling decimalToBinary().

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 21, 2012 Mar 21, 2012
LATEST

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 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines