jump to navigation

Decimals to octals and vice versa August 1, 2007

Posted by maxmil in : Flash , trackback

Just had to create a couple of functions in actionscript to transform base 10 numbers in base 8 and vice versa. Maybe this will be of use again to someone at some time.function octal2Decimal(n) {
var valDecimal:Number = 0;
var exp:Number = 1;
var str:String = String(n);
for (var j = str.length; j>0; j--) {
valDecimal = valDecimal+exp*str.substr(j-1, 1);
exp = exp*8;
}
return valDecimal;
}
function decimal2Octal(n) {
var rem:Number = n;
var octal:String = "";
var div:Number = 262144;
var sub:Number;
var begin:Boolean = false;
for (var i = 6; i>=0; i--) {
sub = Math.floor(rem/div);
if (sub>0 || begin) {
octal += String(sub);
begin = true;
}
rem -= sub*div;
div = div/8;
}
return Number(octal);
}

Comments»

no comments yet - be the first?