Groups | Blog | Home
all groups > flash actionscript > march 2004 >

flash actionscript : there is a easier way to do this?


Varacolaci
3/29/2004 11:08:27 PM
I want to convert a decimal to a hexadecimal n? but it's hard this way..
someone nowing a better way??
I'm using flash 2004 pro

this is the code 4 now---->

function decimalToHexadecimal (number) {
array5.splice ;
array4.splice ;
for (i = 0; i <= number; i ++) {
binary = Math.pow (16, i);
if (binary> number) {
arrayelement = i - 1;
break;
}
array5 [i] = binary;
}
for (j = arrayelement; j>= 0; j --) {
if (j == arrayelement) {
binaryelement = int (number / array5 [j]);
binaryremainder = int (number % array5 [j]);
if (binaryelement == 10) {
binaryelement2 = 'A';
} else if (binaryelement == 11) {
binaryelement2 = 'B';
} else if (binaryelement == 12) {
binaryelement2 = 'C';
} else if (binaryelement == 13) {
binaryelement2 = 'D';
} else if (binaryelement == 14) {
binaryelement2 = 'E';
} else if (binaryelement == 15) {
binaryelement2 = 'F';
} else {
binaryelement2 = binaryelement;
}
} else if (binaryelement == 13) {
binaryelement2 = 'D';
} else if (binaryelement == 14) {
binaryelement2 = 'E';
} else if (binaryelement == 15) {
binaryelement2 = 'F';
} else {
binaryelement2 = binaryelement;
}array4 [arrayelement - j] = binaryelement2;
}
}
return array4.join ("");
}
tralfaz
3/29/2004 11:14:39 PM
It's very impressive coding for you both but it can be a bit easier than
that!

dec = 65535;
trace(dec.toString(16)); // FFFF

The 16 is the radix for hex.
If you want to get four nibbles always you need a little more code..
(instead of just F you might want 000F or 0x000F)

//*********************************
function dectohex(v) {
num = Number(v).toString(16);
while (num.length<4) {
num = '0'+num;
}
return "0x"+num.toUpperCase();
}
//*********************************
trace(dectohex(15)); // 0x000F
trace(dectohex(65535)); // 0xFFFF

tralfaz

Peter Weidauer
3/30/2004 3:16:07 AM
Varacolaci, I must confess I did not understand your code. What for are
those two arrays at the top, what's that splicing supposed to do?

Here is what I would do. Please note that the function below does not
handle zero, negative numbers and floating-point numbers.


trace( changeBase(255, 16) ); // --> FF
trace( changeBase(255, 2) ); // --> 11111111

function changeBase(decimalNumber, base) {
var symbols = "0123456789ABCDEF";
var digits = "";
var pos = 0;
while (decimalNumber > 0) {
var value = decimalNumber % Math.pow(base, pos+1);
decimalNumber -= value;
var digit = value / Math.pow(base, pos);
digits = symbols.charAt(digit) + digits;
pos++;
}
return digits;
}
Peter Weidauer
3/30/2004 2:46:28 PM
No, no... That's definitely too easy!

;-)
tralfaz
3/30/2004 4:09:30 PM
[quoted text, click to view]

Yeah, that can't be right. hehehe


AddThis Social Bookmark Button