JS 만능 임 의 진법 전환 실현 - 진법 상 상한 선 없 음

전단 JS 만능 진법 상호 전환 실현: N 진법 M 진법
Java 버 전:https://blog.csdn.net/qiziyiming/article/details/83443771
 



        


10 : :
N 10 : :

/** * <br> * ,, 62 , , <br> * 256 16 (xx-xx-xx) <br> * 62 (Z~A:61~36;z~a:35~10;9~0) * * @author Administrator * */ /** * 62 */ var bdm62 =[62]; /** * 62 , ,0~9a~zA~Z <br> * 62 */ var bdm62Str = ""; /** * double */ function Hexadecimal() { /** * double */ for (var i = 0; i <= 9; i++) { /* 0~9 */ bdm62[i] =String.fromCharCode((i + 48)); } for (var i = 0; i < 26; i++) { /* a~z */ bdm62[i + 10] =String.fromCharCode((i + 97)); } for (var i = 0; i < 26; i++) { /* A~Z */ bdm62[i + 36] =String.fromCharCode((i + 65)); } for (var i = 0; i < bdm62.length; i++) { bdm62Str+=bdm62[i]; } } function setbtn1(){ var pow=document.getElementById("text1").value; var get=document.getElementById("va1").value; var pos=document.getElementById("va2").value; var sip=document.getElementById("va3").value; setval(pow,get,pos,sip); } function setbtn2(){ var pow=document.getElementById("text2").value; var get=document.getElementById("va4").value; var pos=document.getElementById("va5").value; var st1=document.getElementById("va6").value; if(pow<2){ return ; } if(get<2){ get=16; } if(st1==null|st1==""){ sip="-"; } document.getElementById("a4").innerHTML=pow; /** * N 10 */ var nto10 = getNto10_0(pow,st1,get ,pos ); /** * N 10 */ document.getElementById("a5").innerHTML=nto10; } function setval(pow,get,pos,sip){ if(pow<2){ return ; } if(get<2){ get=16; } if(sip==null|sip==""){ sip="-"; } var get10ToN =""; /** * double */ document.getElementById("a1").innerHTML=pow; /** * 10 N */ get10ToN = get10ToN_10(pow,get,pos,sip,true); /** * 10 N */ document.getElementById("a2").innerHTML=get10ToN; /** * N 10 */ var nto10 = getNto10_0(get10ToN,sip, get, pos); /** * N 10 */ document.getElementById("a3").innerHTML=nto10; } /** * */ function test() { /** * */ var get=32; /** * */ var pos=0; /** * */ var pow = Math.pow(255,13); setval(pow,get,pos,"-"); } /** * @see #getNto10(String,String,var,var) * @param name {@link #getNto10(String, String, var, var)} * @param now {@link #getNto10(String, String, var, var)} * @param pos {@link #getNto10(String, String, var, var)} * @return * @throws Exception */ function getNto10(name, now, pos){ return getNto10_0(name,"-",now,pos); } /** * N 10 * * @param name * N <br> * xxxnxnxnn( , 62 Aa0) <br> * xn-xn-xx-nn( , <br> * 256 16 (xx-xx-xx)) * @param nameSplit * * @param now * * @param pos * , 62(pos<2 62) * @return */ function getNto10_0(name, nameSplit, now, pos){ /** * */ if (pos > 62) { pos = 62; } else if (pos < 2) { pos = 62; } /** * */ var m = 0; /** * */ if (now > pos) { /** * */ var k = 0; /** * * 256 16 , 2 16 * */ var lontem =lon(now,pos); k=lon; /** * */ if(lontem>k){ k++; } /** * , , */ var arrayList = new Array(); var arrayList_i=0; /** * */ if (name.indexOf(nameSplit) > 0||name.length<k) { var split = name.split(nameSplit); /** * 10 */ for (var i = 0; i < split.length; i++) { /** * , , */ if (split[i].length > k) { alert(name + "\t(" + split[i] + ") , :" + k); } /** * , 10 */ var setHdl10 = getNto10_0(split[i],nameSplit, pos, 0); /** * */ arrayList[arrayList_i++]=(setHdl10); } } else { /** * , */ var i=0; while(true){ /** * , k */ if(name.length>(i + 1) * k){ var substring=name.substring(i * k, (i + 1) * k); var setHdl10 = getNto10(substring,nameSplit, pos, 0); arrayList[arrayList_i++]=(setHdl10); }else { /** * */ var substring=name.substring(i * k,name.length); var setHdl10 = getNto10(substring,nameSplit, pos, 0); arrayList[arrayList_i++]=(setHdl10); break ; } i++; } } /** * 10 * :34567 * 7+0 * 7+0+6*10 * 7+0+6*10+5*100 * 7+0+6*10+5*100+4*1000 * 7+0+6*10+5*100+4*1000+3*10000=34567 */ var length = arrayList.length - 1; for (var i = length; i >= 0; i--) { var double1 = arrayList[i]; /** * */ var pow = Math.pow(now, length - i); m += double1 * pow; } } else { /** * * 10 :0~9 * 16 :0~9a~f */ if (!getSpecial(name, now)) { var length = name.length - 1; /** * * 16 :adb467 * 7+0 * 7+0+6*16 * 7+0+ 6*16+ 4*16*16 * 7+0+ 6*16+ 4*16*16+ 11[b]*16*16*16 * 7+0+ 6*16+ 4*16*16+ 11[b]*16*16*16+ 13[d]*16(4 ) * ...... */ for (var i = length; i >= 0; i--) { /** * */ var charAt = name.charAt(i); /** * , */ var indexOf = bdm62Str.indexOf(charAt); /** * */ var pow = Math.pow(now, length - i); /** * 0, */ if(indexOf>0){ m += indexOf * pow; } } } else { alert(name + " " + now + " :" + bdm62Str.substring(0, now)); } } return m; } /** * @see #get10ToN(double, double, double, String) * @param name * @param get * @param pos * @return * @throws Exception */ function get10ToN(name,get,pos){ return get10ToN_10(name, get, pos, null,true); } /** * N * @param name * @param get N * @param pos * @param nameSplit , * @param isReverse * @return * @throws Exception */ function get10ToN_10(name,get,pos,nameSplit,isReverse) { var split = nameSplit==null||nameSplit==""; if(get>pos && split){ alert(" "); } if(name<0){ name*=-1; } /** * */ var stringBuilder = ""; /** * */ if (pos > 62) { pos = 62; } else if (pos < 2) { pos = 62; } if(get>pos){ /** * get */ var d = lon(name,get); if(Math.round(d)==d){ for (var i = 0; i < d; i++) { stringBuilder+='0'; stringBuilder+=nameSplit; } stringBuilder+='1'; }else { /** * */ while (name>0) { /** * */ d = name%get; name-=d; if(d>0){ /** * , */ var strTem = get10ToN_10(d, pos, pos, nameSplit,false); stringBuilder+=strTem; /** * */ if(name>0){ stringBuilder+=nameSplit; } }else { stringBuilder+='0'; /** * */ if(name>0){ stringBuilder+=nameSplit; } } name/=get; } } }else { /** * get */ var d = lon(name,get); /** * */ if(Math.round(d)==d){ for (var i = 0; i < d; i++) { stringBuilder+='0'; } stringBuilder+='1'; }else { /** * * */ while (name>0) { /** * :1000*60*60*10=10 =3600000 * 3600000%1000 3600000%1000 * (3600000-3600000%1000)/1000=3600 3600000%1000 * (3600-3600%60)/60=600 3600%60 * (600-600%60)/60=10 */ d =name%get; name-=d; stringBuilder+=bdm62Str.charAt(d); name/=get; } } } if(isReverse){ /** * , , */ var strtem=""; for(var i=stringBuilder.length-1;i>=0;i--){ strtem+=stringBuilder.charAt(i); } return strtem; }else { return stringBuilder; } } /** * * @param value * @param base * @return */ function lon(value, base){ return Math.log(value)/Math.log(base); } /** * K * * @param name * @param k * @return */ function getSpecial(name, k){ var substring = bdm62Str.substring(0, k); var matches =RegExp( "/^[" + substring + "]*$/"); return getSpecial0(name, matches); } /** * * * @param name * * @param matches * * * @return */ function getSpecial0(name, matches) { return matches.test(name); } Hexadecimal(); test();

 
 
 

좋은 웹페이지 즐겨찾기