扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
下面是偶在JavaScript中实现的各种进制间的转换。
<script language="javascript">
//dh20156
//2006-9-10
//返回指定数量的重复字符组成的字符串;
function dhString(n,c){
var tempStr = "";
for(var i=0;i<n;i++){
tempStr += c.toString();
}
return tempStr;
}
document.write("二进制、十进制转换<hr>");
//二进制 --> 十进制
function tw2teChange(n){
var strN = n.toString();
var ln = strN.length;
var tempInt = 0;
for(var i=0;i<ln;i++){
var t = strN.substr(i,1);
var e = ln-i-1;
if(!isNaN(t)){
tempInt += t*(Math.pow(2,e));
}else{
tempInt += "?";
}
}
return tempInt;
}
function tw2te(n){
var strN = n.toString();
var tempInt = 0;
var ln = strN.length;
var ld = strN.indexOf(".")
if(ld<0){
tempInt = tw2teChange(strN);
}else{
var ti = strN.substring(0,ld);
var tm = strN.substring(ld+1,ln);
var tempInt1 = tw2teChange(ti);
var tempInt2 = tw2teChange(tm);
tempInt = tempInt1+"."+tempInt2;
}
return tempInt;
}
//------ 结束 -----
var a = "10110111.101100";
document.write(a+" --2T10--> "+tw2te(a)+"<br>");
//十进制 --> 二进制
function te2twChange(n){
if(!isNaN(n) && n>0){
if(n%2==0){
return te2twChange(n/2)+"0";
}else{
if(n>2){
return te2twChange(parseInt(n/2))+(n%2);
}else{
return te2twChange(0)+n;
}
}
}else{
return "";
}
}
function te2tw(n){
if(!isNaN(n)){
var strN = n.toString();
var tempInt = 0;
var ln = strN.length;
var ld = strN.indexOf(".")
if(ld<0){
tempInt = te2twChange(n);
}else{
var ti = strN.substring(0,ld);
var tm = strN.substring(ld+1,ln);
var tempInt1 = te2twChange(ti);
var tempInt2 = te2twChange(tm);
tempInt = tempInt1+"."+tempInt2;
}
return tempInt;
}else{
return "false";
}
}
//------ 结束 -----
var b = 101;
document.write(b+" --10T2--> "+te2tw(b)+"<br>");
document.write("<br>二进制、八进制转换<hr>");
//二进制 --> 八进制
function tw2eiChange(n,tp){
var strN = n.toString();
var ln = strN.length;
var tempInt = "";
if(tp=="intType"){
for(var i=ln;i>0;i -= 3){
var tempN = strN.substring(i-3,i);
tempInt = tw2te(tempN)+tempInt;
}
}else{
for(var i=0;i<ln;i += 3){
var tempN = strN.substring(i,i+3);
if(tempN.length<3){
tempN = tempN+dhString((3-tempN.length),"0");
}
tempInt += tw2te(tempN);
}
}
return tempInt;
}
function tw2ei(n){
var strN = n.toString();
var tempInt = 0;
var ln = strN.length;
var ld = strN.indexOf(".")
if(ld<0){
tempInt = tw2eiChange(strN,"intType");
}else{
var ti = strN.substring(0,ld);
var tm = strN.substring(ld+1,ln);
var tempInt1 = tw2eiChange(ti,"intType");
var tempInt2 = tw2eiChange(tm,"modType");
tempInt = tempInt1+"."+tempInt2;
}
return tempInt;
}
//------ 结束 -----
var c = "10110111.1011";
document.write(c+" --2T8--> "+tw2ei(c)+"<br>");
//八进制 --> 二进制
function ei2twChange(n){
var strN = n.toString();
var ln = strN.length;
var tempInt = "";
for(var i=0;i<ln;i++){
var t = strN.substr(i,1);
var e = ln-i-1;
if(!isNaN(t)){
tempInt += te2tw(t);
}else{
tempInt += "?";
}
}
return tempInt;
}
function ei2tw(n){
if(!isNaN(n)){
var strN = n.toString();
var tempInt = 0;
var ln = strN.length;
var ld = strN.indexOf(".")
if(ld<0){
tempInt = ei2twChange(n);
}else{
var ti = strN.substring(0,ld);
var tm = strN.substring(ld+1,ln);
var tempInt1 = ei2twChange(ti);
var tempInt2 = ei2twChange(tm);
tempInt = tempInt1+"."+tempInt2;
}
return tempInt;
}else{
return "false";
}
}
//------ 结束 -----
var d = 267.54;
document.write(d+" --8T2--> "+ei2tw(d)+"<br>");
document.write("<br>二进制、十六进制转换<hr>");
//二进制 <-匹配表-> 十六进制
function twMatchst(v,rv){
if(rv==16){
switch(v.toString()){
case "0000":return "0";break;
case "0001":return "1";break;
case "0010":return "2";break;
case "0011":return "3";break;
case "0100":return "4";break;
case "0101":return "5";break;
case "0110":return "6";break;
case "0111":return "7";break;
case "1000":return "8";break;
case "1001":return "9";break;
case "1010":return "A";break;
case "1011":return "B";break;
case "1100":return "C";break;
case "1101":return "D";break;
case "1110":return "E";break;
case "1111":return "F";break;
default:return "";break;
}
}else{
switch(v.toString()){
case "0":return "0000";break;
case "1":return "0001";break;
case "2":return "0010";break;
case "3":return "0011";break;
case "4":return "0100";break;
case "5":return "0101";break;
case "6":return "0110";break;
case "7":return "0111";break;
case "8":return "1000";break;
case "9":return "1001";break;
case "A":return "1010";break;
case "B":return "1101";break;
case "C":return "1100";break;
case "D":return "1101";break;
case "E":return "1110";break;
case "F":return "1111";break;
default:return "";break;
}
}
}
//------ 结束 ------
//二进制 --> 十六进制
function tw2stChange(n,tp){
var strN = n.toString();
var ln = strN.length;
var tempInt = "";
if(tp=="intType"){
for(var i=ln;i>0;i -= 4){
var tempN = strN.substring(i-4,i);
if(tempN.length<4){
tempN = dhString((4-tempN.length),"0")+tempN;
}
tempInt = twMatchst(tempN,16)+tempInt;
}
}else{
for(var i=0;i<ln;i += 4){
var tempN = strN.substring(i,i+4);
if(tempN.length<4){
tempN = tempN+dhString((4-tempN.length),"0");
}
tempInt += twMatchst(tempN,16);
}
}
return tempInt;
}
function tw2st(n){
var strN = n.toString();
var tempInt = 0;
var ln = strN.length;
var ld = strN.indexOf(".")
if(ld<0){
tempInt = tw2stChange(strN,"intType");
}else{
var ti = strN.substring(0,ld);
var tm = strN.substring(ld+1,ln);
var tempInt1 = tw2stChange(ti,"intType");
var tempInt2 = tw2stChange(tm,"modType");
tempInt = tempInt1+"."+tempInt2;
}
return tempInt;
}
//------ 结束 ------
var e = "110110111.101001";
document.write(e+" --2T16--> "+tw2st(e)+"<br>");
//十六进制 --> 二进制
function st2twChange(n){
var strN = n.toString();
var ln = strN.length;
var tempInt = "";
for(var i=0;i<ln;i++){
var t = strN.substr(i,1);
var e = ln-i-1;
tempInt += twMatchst(t,2);
}
return tempInt;
}
function st2tw(n){
var strN = n.toString();
var tempInt = 0;
var ln = strN.length;
var ld = strN.indexOf(".")
if(ld<0){
tempInt = st2twChange(strN);
}else{
var ti = strN.substring(0,ld);
var tm = strN.substring(ld+1,ln);
var tempInt1 = st2twChange(ti);
var tempInt2 = st2twChange(tm);
tempInt = tempInt1+"."+tempInt2;
}
return tempInt;
}
//------ 结束 ------
var f = "1B7.A4";
document.write(f+" --16T2--> "+st2tw(f)+"<br>");
document.write("<br>八进制、十进制转换<hr>");
//八进制 --> 十进制
function ei2teChange(n){
var strN = n.toString();
var ln = strN.length;
var tempInt = 0;
for(var i=0;i<ln;i++){
var t = strN.substr(i,1);
var e = ln-i-1;
if(!isNaN(t)){
tempInt += t*(Math.pow(8,e));
}else{
tempInt += "?";
}
}
return tempInt;
}
function ei2te(n){
var strN = n.toString();
var tempInt = 0;
var ln = strN.length;
var ld = strN.indexOf(".")
if(ld<0){
tempInt = ei2teChange(strN);
}else{
var ti = strN.substring(0,ld);
var tm = strN.substring(ld+1,ln);
var tempInt1 = ei2teChange(ti);
var tempInt2 = ei2teChange(tm);
tempInt = tempInt1+"."+tempInt2;
}
return tempInt;
}
//------ 结束 -----
var g = "267.54";
document.write(g+" --8T10--> "+ei2te(g)+"<br>");
//十进制 --> 八进制
function te2eiChange(n){
if(!isNaN(n) && n>0){
if(n%8==0){
return te2eiChange(n/8)+"0";
}else{
if(n>8){
return te2eiChange(parseInt(n/8))+(n%8);
}else{
return te2eiChange(0)+n;
}
}
}else{
return "";
}
}
function te2ei(n){
if(!isNaN(n)){
var strN = n.toString();
var tempInt = 0;
var ln = strN.length;
var ld = strN.indexOf(".")
if(ld<0){
tempInt = te2eiChange(n);
}else{
var ti = strN.substring(0,ld);
var tm = strN.substring(ld+1,ln);
var tempInt1 = te2eiChange(ti);
var tempInt2 = te2eiChange(tm);
tempInt = tempInt1+"."+tempInt2;
}
return tempInt;
}else{
return "false";
}
}
//------ 结束 -----
var h = 183.44;
document.write(h+" --10T8--> "+te2ei(h)+"<br>");
document.write("<br>十六进制、十进制转换<hr>");
//十进制 <-匹配表-> 十六进制
function teMatchst(v,rv){
if(rv==16){
switch(v.toString()){
case "0":return "0";break;
case "1":return "1";break;
case "2":return "2";break;
case "3":return "3";break;
case "4":return "4";break;
case "5":return "5";break;
case "6":return "6";break;
case "7":return "7";break;
case "8":return "8";break;
case "9":return "9";break;
case "10":return "A";break;
case "11":return "B";break;
case "12":return "C";break;
case "13":return "D";break;
case "14":return "E";break;
case "15":return "F";break;
default:return "";break;
}
}else{
switch(v.toString()){
case "0":return "0";break;
case "1":return "1";break;
case "2":return "2";break;
case "3":return "3";break;
case "4":return "4";break;
case "5":return "5";break;
case "6":return "6";break;
case "7":return "7";break;
case "8":return "8";break;
case "9":return "9";break;
case "A":return "10";break;
case "B":return "11";break;
case "C":return "12";break;
case "D":return "13";break;
case "E":return "14";break;
case "F":return "15";break;
default:return "";break;
}
}
}
//------ 结束 ------
//十六进制 --> 十进制
function st2teChange(n){
var strN = n.toString();
var ln = strN.length;
var tempInt = 0;
for(var i=0;i<ln;i++){
var t = strN.substr(i,1);
t = teMatchst(t,10)
var e = ln-i-1;
if(!isNaN(t)){
tempInt += t*(Math.pow(16,e));
}else{
tempInt += "?";
}
}
return tempInt;
}
function st2te(n){
var strN = n.toString();
var tempInt = 0;
var ln = strN.length;
var ld = strN.indexOf(".")
if(ld<0){
tempInt = st2teChange(strN);
}else{
var ti = strN.substring(0,ld);
var tm = strN.substring(ld+1,ln);
var tempInt1 = st2teChange(ti);
var tempInt2 = st2teChange(tm);
tempInt = tempInt1+"."+tempInt2;
}
return tempInt;
}
//------ 结束 -----
var i = "A4B7";
document.write(i+" --16T10--> "+st2te(i)+"<br>");
//十进制 --> 十六进制
function te2stChange(n){
if(!isNaN(n) && n>0){
if(n%16==0){
return te2stChange(n/16)+"0";
}else{
if(n>16){
return te2stChange(parseInt(n/16))+teMatchst((n%16),16);
}else{
return te2stChange(0)+teMatchst(n,16);
}
}
}else{
return "";
}
}
function te2st(n){
if(!isNaN(n)){
var strN = n.toString();
var tempInt = 0;
var ln = strN.length;
var ld = strN.indexOf(".")
if(ld<0){
tempInt = te2stChange(n);
}else{
var ti = strN.substring(0,ld);
var tm = strN.substring(ld+1,ln);
var tempInt1 = te2stChange(ti);
var tempInt2 = te2stChange(tm);
tempInt = tempInt1+"."+tempInt2;
}
return tempInt;
}else{
return "false";
}
}
//------ 结束 -----
var j = 120;
document.write(j+" --10T16--> "+te2st(j)+"<br>");
</script>
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者