2005/06/23 | 我的Scale类
类别(Flash) | 评论(2) | 阅读(653) | 发表于 18:22
FLASH中没在直接的进行数制之间转换的函数,今天有时间写了一下:

/********************************************************************************
*类名:myMath.Scale
*功能:实现不同进制之间的转换
*用法:myTest = new Scale();
* bin = 11111;
* trace(myTest.binTOoct(bin));
*作者:西部秋虫(hdt1976@tom.com)
*版权:西部之光Software,如需转载,请与作者联系.
********************************************************************************/
class myMath.Scale extends Math {
//初始化
private var Result_Str:String;
private var Result_Num:Number;
private var tmpArray:Array;
private var Ele:String;
private var tmpEle:String;
private var toString:String;
//十进制转化为二进制
function decTObin(para) {
Result_Str = "";
tmpArray = new Array();
while (para>=1) {
Ele = String(para%2);
para = int(para/2);
tmpArray.push(Ele);
}
tmpArray.reverse();
for (var i = 0; i<tmpArray.length; i++) {
Result_Str += tmpArray[i];
}
return (Result_Str);
}
//十进制转化为八进制
function decTOoct(para) {
Result_Str = "";
tmpArray = new Array();
while (para>=1) {
Ele = String(para%8);
para = int(para/8);
tmpArray.push(Ele);
}
tmpArray.reverse();
for (var i = 0; i<tmpArray.length; i++) {
Result_Str += tmpArray[i];
}
return (Result_Str);
}
//十进制转化为十六进制
function decTOhex(para) {
Result_Str = "";
tmpArray = new Array();
while (para>=1) {
Ele = String(para%16);
switch (Ele) {
case "10" :
Ele = "A";
break;
case "11" :
Ele = "B";
break;
case "12" :
Ele = "C";
break;
case "13" :
Ele = "D";
break;
case "14" :
Ele = "E";
break;
case "15" :
Ele = "F";
break;
}
para = int(para/16);
tmpArray.push(Ele);
}
tmpArray.reverse();
for (var i = 0; i<tmpArray.length; i++) {
Result_Str += tmpArray[i];
}
return (Result_Str);
}
//二进制转化为八进制
function binTOoct(para) {
Result_Num = this.decTOoct(this.binTOdec(para));
return (Result_Num);
}
//二进制转化为十进制
function binTOdec(para) {
Result_Num = 0;
toString = String(para);
for (var i = 0; i<toString.length; i++) {
tmpEle = toString.charAt(i);
Result_Num += Number(tmpEle)*Math.pow(2, toString.length-1-i);
}
return (Result_Num);
}
//二进制转化为十六进制
function binTOhex(para) {
Result_Num = this.decTOhex(this.binTOdec(para));
return (Result_Num);
}
//八进制转化为二进制
function octTObin(para) {
Result_Num = this.decTObin(this.octTOdec(para));
return (Result_Num);
}
//八进制转化为十进制
function octTOdec(para) {
Result_Num = 0;
toString = String(para);
for (var i = 0; i<toString.length; i++) {
tmpEle = toString.charAt(i);
Result_Num += Number(tmpEle)*Math.pow(8, toString.length-1-i);
}
return (Result_Num);
}
//八进制转化为十六进制
function octTOhex(para) {
Result_Num = this.decTOhex(this.octTOdec(para));
return (Result_Num);
}
//十六进制转化为二进制
function hexTObin(para) {
Result_Num = this.decTObin(this.hexTOdec(para));
return (Result_Num);
}
//十六进制转化为十进制
function hexTOdec(para) {
Result_Num = 0;
toString = String(para);
for (var i = 0; i<toString.length; i++) {
tmpEle = toString.charAt(i);
switch ((tmpEle).toUpperCase()) {
case "A" :
tmpEle = "10";
break;
case "B" :
tmpEle = "11";
break;
case "C" :
tmpEle = "12";
break;
case "D" :
tmpEle = "13";
break;
case "E" :
tmpEle = "14";
break;
case "F" :
tmpEle = "15";
break;
}
Result_Num += Number(tmpEle)*Math.pow(16, toString.length-1-i);
}
return (Result_Num);
}
//十六进制转化为八进制
function hexTOoct(para) {
Result_Num = this.decTOoct(this.hexTOdec(para));
return (Result_Num);
}
}


示例:

import myMath.Scale;
myTest = new Scale();
bin = 11111;
trace("原始数:"+bin);
trace("二进制向八进制转换:"+myTest.binTOoct(bin));
trace("二进制向十进制转换:"+myTest.binTOdec(bin));
trace("二进制向十六进制转换:"+myTest.binTOhex(bin));
oct = 777111;
trace("原始数:"+oct);
trace("八进制向二进制转换:"+myTest.octTObin(oct));
trace("八进制向十进制转换:"+myTest.octTOdec(oct));
trace("八进制向十六进制转换:"+myTest.octTOhex(oct));
dec = 100;
trace("原始数:"+dec);
trace("十进制向二进制转换:"+myTest.decTObin(dec));
trace("十进制向八进制转换:"+myTest.decTOoct(dec));
trace("十进制向十六进制转换:"+myTest.decTOhex(dec));
//十六进制应为字符串
hex = "ffff";
trace("原始数:"+hex);
trace("十六进制向二进制转换:"+myTest.hexTObin(hex));
trace("十六进制向八进制转换:"+myTest.hexTOoct(hex));
trace("十六进制向十进制转换:"+myTest.hexTOdec(hex));
//恶搞一下
trace("恶搞一下...");
trace(myTest.decTObin(myTest.hexTOdec("ffFF")));
trace(myTest.decTOhex(myTest.hexTOdec("ffFF")));
0

评论Comments

日志分类
首页[186]
Flash[84]
FMS[41]
AIR[2]
ASP[11]
作品区[12]
其他[36]