2 Replies - 1702 Views - Last Post: 19 March 2012 - 11:22 AM

#1 adamkay123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-October 11

binary conversion

Posted 19 March 2012 - 06:47 AM

Im having a problem with this code to convert decimal to binary ive got the formula but it wont print to the screen_txt only on the console and also my calculator wont let me change any other number other than 3 so if i press 7 binary should be 111 but it stays 11...

var num: Number;
var inputVal:Number;
var calculatedVal:Number;

binary_btn.addEventListener(MouseEvent.CLICK, binaryBtn);

function binaryBtn(event:MouseEvent) : void 
{
inputVal = Number(screen_txt.text);
var num:Number = parseInt (, 10);
trace (num.toString(2));
trace(inputVal);
calculatedVal = Number(num);
screen_txt.text = calculatedVal.toString();
}



Is This A Good Question/Topic? 0
  • +

Replies To: binary conversion

#2 adamkay123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-October 11

Re: binary conversion

Posted 19 March 2012 - 07:46 AM

function binary(event:MouseEvent) : void
{
          var num:Number = parseInt ("3", 10);
          trace (num.toString(2));
          output_txt.text =Number(output_txt.text).toString(2)
 
}
 
this basically converts decimal 3 into binary 11
and shows it on console
ive got my calculator and the display screen is output_txt and its not showing up on that
people have told me to use parseInt and trace but i have no idea how to go about it?? thats why i thought it might be similar to this
 
var num: Number;
var inputVal:Number;
var calculatedVal:Number;

binary_btn.addEventListener(MouseEvent.CLICK, binaryBtn);

function binaryBtn(event:MouseEvent) : void
{
        inputVal = Number(screen_txt.text);
        var num:Number = parseInt (, 10);
        trace (num.toString(2));
        trace(inputVal);
        calculatedVal = Number(num);
    screen_txt.text = calculatedVal.toString();
}


Was This Post Helpful? 0
  • +
  • -

#3 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: binary conversion

Posted 19 March 2012 - 11:22 AM

parseInt() is used to convert a string into an integer. You need to pass a string to it which you want to convert. So in your case it would be screen_txt.text.

var your_number:int = parseInt(screen_txt.text,10);
// No need to cast to Number anymore, parseInt does that for your


Finally to get binary of it, you use toString() on the integer you got and pass 2 to get binary:

var binary_string:String = your_number.toString(2);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1