Skip to main content
Participating Frequently
March 19, 2012
Question

binary conversion

  • March 19, 2012
  • 2 replies
  • 663 views

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();

}

This topic has been closed for replies.

2 replies

kglad
Community Expert
Community Expert
March 19, 2012

use:


var num: Number;

var inputVal:Number;

var calculatedVal:Number;


binary_btn.addEventListener(MouseEvent.CLICK, binaryBtn);


function binaryBtn(event:MouseEvent) : void

{

screen_txt.text =Number(screen_txt.text).toString(2)

}

Participating Frequently
March 19, 2012

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();

}

kglad
Community Expert
Community Expert
March 19, 2012

use the code i suggested if you want the binary representation to display in screen_txt.  if you want it to display in a different textfield, replace screen_txt in my code with whatever.

_spoboyle
Inspiring
March 19, 2012

so it prints correctly to the console but doesn't display correctly inthe textfield?

it the textfield big enough to display the whole number?