Previous Tip  |  Next Tip  |  Design Tips   | [Bill's Home]

# 116. To hexademical, and beyond

[Expand] Some time ago I showed how to generate binary conversions, but I did not convert to hexademical. I've thus revised the movie so that it now includes an automatic conversion to binary and hexademical:

For this I included the hexademical conversion function of:

 function tohex(val) { var i:int; str=""; for (bit=0;bit<2;bit++) {  j=int(val%16);  if (j==0) str=str+"0";  else if (j==1) str=str+"1";  else if (j==2) str=str+"2";  else if (j==3) str=str+"3";  else if (j==4) str=str+"4";  else if (j==5) str=str+"5";  else if (j==6) str=str+"6";  else if (j==7) str=str+"7";  else if (j==8) str=str+"8";  else if (j==9) str=str+"9";  else if (j==10) str=str+"A";  else if (j==11) str=str+"B";  else if (j==12) str=str+"C";  else if (j==13) str=str+"D";  else if (j==14) str=str+"E";  else if (j==15) str=str+"F";  val=int(val/16);  trace(val); } tmp=str; str=""; for (i=7;i>=0;i--) {  str=str+tmp.slice(i,i+1); } return(str); }

The code is initially called with:

 dec1_val = random(255); hex1_val = tohex(dec1_val); bin1_val = tobinary(dec1_val); dec2_val = random(255); hex2_val = tohex(dec2_val); bin2_val = tobinary(dec2_val); dec3_val = random(255); hex3_val = tohex(dec3_val); bin3_val = tobinary(dec3_val); dec4_val = random(255); hex4_val = tohex(dec4_val); bin4_val = tobinary(dec4_val); dec5_val = random(255); hex5_val = tohex(dec5_val); bin5_val = tobinary(dec5_val); dec6_val = random(255); hex6_val = tohex(dec6_val); bin6_val = tobinary(dec6_val); dec1=dec1_val; dec2="x"; dec3="x"; dec4=dec4_val; dec5="x"; dec6="x"; hex1="x"; hex2=tohex(dec2_val); hex3="x"; hex4="x"; hex5=tohex(dec5_val); hex6="x"; bin1="xxxxxxxx"; bin2="xxxxxxxx"; bin3=tobinary(dec3_val); bin4="xxxxxxxx"; bin5="xxxxxxxx"; bin6=tobinary(dec6_val); function tobinary(val) { var i:int; str=""; for (bit=0;bit<8;bit++) { j=int(val%2); if (j==0) str=str+"0"; else str=str+"1"; val=int(val/2); trace(val); } tmp=str; str=""; for (i=7;i>=0;i--) { trace(tmp.slice(i,i+1)); str=str+tmp.slice(i,i+1); } return(str); } function tohex(val) { var i:int; str=""; for (bit=0;bit<2;bit++) { j=int(val%16); if (j==0) str=str+"0"; else if (j==1) str=str+"1"; else if (j==2) str=str+"2"; else if (j==3) str=str+"3"; else if (j==4) str=str+"4"; else if (j==5) str=str+"5"; else if (j==6) str=str+"6"; else if (j==7) str=str+"7"; else if (j==8) str=str+"8"; else if (j==9) str=str+"9"; else if (j==10) str=str+"A"; else if (j==11) str=str+"B"; else if (j==12) str=str+"C"; else if (j==13) str=str+"D"; else if (j==14) str=str+"E"; else if (j==15) str=str+"F"; val=int(val/16); trace(val); } tmp=str; str=""; for (i=7;i>=0;i--) { trace(tmp.slice(i,i+1)); str=str+tmp.slice(i,i+1); } return(str); }