4 Replies Latest reply on Mar 19, 2012 8:12 AM by kglad

# binary conversion

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;

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

}

• ###### 1. Re: binary conversion

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

it the textfield big enough to display the whole number?

• ###### 2. Re: binary conversion

use:

var num: Number;

var inputVal:Number;

var calculatedVal:Number;

function binaryBtn(event:MouseEvent) : void

{

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

}

• ###### 3. Re: binary conversion

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;

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

}

• ###### 4. Re: binary conversion

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.