js的進(jìn)制轉(zhuǎn)換, 分為2進(jìn)制,8進(jìn)制,10進(jìn)制,16進(jìn)制之間的相互轉(zhuǎn)換, 我們直接利用 對(duì)象.toString()即可實(shí)現(xiàn):
運(yùn)行下面代碼
//10進(jìn)制轉(zhuǎn)為16進(jìn)制(10).toString(16) // =>"a"http://8進(jìn)制轉(zhuǎn)為16進(jìn)制(012).toString(16) // =>"a"http://16進(jìn)制轉(zhuǎn)為10進(jìn)制(0x16).toString(10) // =>"22"http://16進(jìn)制轉(zhuǎn)為8進(jìn)制(0x16).toString(8) // =>"26"http://10進(jìn)制轉(zhuǎn)為2進(jìn)制 //=>(1111).toString(2) // => "10001010111"http://8進(jìn)制轉(zhuǎn)為2進(jìn)制 //=>(01111).toString(2) //=>"1001001001"http://16進(jìn)制轉(zhuǎn)為2進(jìn)制 //=>(0x16).toString(2) // => "10110"
如果要處理2進(jìn)制到10進(jìn)制,16進(jìn)制到10進(jìn)制,8進(jìn)制到10進(jìn)制, 需要用了paresInt這個(gè)方法:
運(yùn)行下面代碼
//2進(jìn)制到10進(jìn)制;parseInt(10,2) //=>2//2進(jìn)制到10進(jìn)制;parseInt(100,2) //=>4//16進(jìn)制到10進(jìn)制parseInt(12, 16) //=>18//8進(jìn)制到10進(jìn)制parseInt(12,8); //=>10
進(jìn)制轉(zhuǎn)換
如果要實(shí)現(xiàn)進(jìn)制之間的轉(zhuǎn)換, 可以利用parseInt方法, 先轉(zhuǎn)化為10進(jìn)制, 然后再利用toString(參數(shù)), 轉(zhuǎn)化成不同的進(jìn)制;
利用toString和parseInt方法可以實(shí)現(xiàn)一個(gè)進(jìn)制轉(zhuǎn)化的工具:
運(yùn)行下面代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標(biāo)題文檔</title></head><body><script language="javascript">function test(){var num=document.getElementById("in").value;var type=document.getElementById("title");var tynum,to;for(var i=0;i<type.length;i++){if(type[i].selected)tynum=parseInt(type[i].value);}switch(tynum){case(1):to=parseInt(num).toString(2);break;case(2):to=parseInt(num).toString(8);break;case(3):to=parseInt(num).toString(16);break;case(4):to=parseInt(num,2);break;case(5):to=parseInt(num,8);break;case(6):to=parseInt(num,16);break;case(7):to=parseInt(num,2).toString(8);break;case(8):to=parseInt(num,8).toString(2);break;case(9):to=parseInt(num,2).toString(16);break;case(10):to=parseInt(num,16).toString(2);break;case(11):to=parseInt(num,8).toString(16);break;case(12):to=parseInt(num,16).toString(8);break;}if(isNaN(to))to="輸入非法字符了哦"document.getElementById("out").value=to;}</script><select name="title" id="title" style="width:152px;"><option value="1">十進(jìn)制轉(zhuǎn)二進(jìn)制</option><option value="2">十進(jìn)制轉(zhuǎn)八進(jìn)制</option><option value="3">十進(jìn)制轉(zhuǎn)十六進(jìn)制</option><option value="4">二進(jìn)制轉(zhuǎn)十進(jìn)制</option><option value="5">八進(jìn)制轉(zhuǎn)十進(jìn)制</option><option value="6">十六進(jìn)制轉(zhuǎn)十進(jìn)制</option><option value="7">二進(jìn)制轉(zhuǎn)八進(jìn)制</option><option value="8">八進(jìn)制轉(zhuǎn)二進(jìn)制</option><option value="9">二進(jìn)制轉(zhuǎn)十六進(jìn)制</option><option value="10">十六進(jìn)制轉(zhuǎn)二進(jìn)制</option><option value="11">八進(jìn)制轉(zhuǎn)十六進(jìn)制</option><option value="12">十六進(jìn)制轉(zhuǎn)八進(jìn)制</option></select><br /><input type="text" id="in" /><br><input type="text" id="out" /><br/><input type="button" value="change" onclick="test()" /><font color="#FF0000" style="font-size:12px;">*注:存在非法字符時(shí),我們只截?cái)嘤行ё址M(jìn)行轉(zhuǎn)換</font></body></html>簡(jiǎn)單加密解密
把字符串轉(zhuǎn)化成unicode, 然后再把unicode轉(zhuǎn)成不同的進(jìn)制 , 實(shí)現(xiàn)代碼加密處理:
運(yùn)行下面代碼
<!DOCTYPE html><html><head><meta charset="utf-8"/><title></title></head><body><script>function en(code, h){//簡(jiǎn)單的jS加密解密<br>//code為對(duì)應(yīng)的字符串,h為(2,8,10,16)就是要轉(zhuǎn)成的幾進(jìn)制<br>function en(code, h) {var monyer = new Array();var i;for(i=0;i<code.length;i++)monyer+=code.charCodeAt(i).toString(h)+"_";//就是把字符串轉(zhuǎn)成ascll碼,然后再轉(zhuǎn)成你想的幾進(jìn)制return monyer;};function de(code, h) {var i,s="",code = code.split("_");for(i=0;i<code.length;i++) {s += String.fromCharCode(parseInt(code[i],h));};return s};en("1哇哈哈",8) //=> "61_52307_52310_52310_"de("61_52307_52310_52310_",8) //=> "1哇哈哈</script></body></html>零寬字符
利用零寬字符的零寬度, 我們把所有的字符串轉(zhuǎn)化成二進(jìn)制, 然后利用零寬字符進(jìn)行表示, 那么生成的字符串長(zhǎng)度就會(huì)為0, 主要反編譯即可還原,
運(yùn)行下面代碼
<!DOCTYPE html><html><head><meta charset="utf-8"/><title></title></head><body><script>function en(str) {var rep = {'00': '/u200b','01': '/u200c','10': '/u200d','11': '/uFEFF'};str = str.replace(/[^/x00-/xff]/g, function (a) { // 轉(zhuǎn)碼 Latin-1 編碼以外的字符。return escape(a).replace('%', '//');});str = str.replace(/[/s/S]/g, function (a) { // 處理二進(jìn)制數(shù)據(jù)并且進(jìn)行數(shù)據(jù)替換a = a.charCodeAt().toString(2);a = a.length < 8 ? Array(9 - a.length).join('0') + a : a;return a.replace(/../g, function (a) {return rep[a];});});return str;};function de(str) {return unescape(str.replace(/.{4}/g, function (a) {var rep = {"/u200b": "00", "/u200c": "01", "/u200d": "10", "/uFEFF": "11"};return String.fromCharCode(parseInt(a.replace(/./g, function (a) {return rep[a]}), 2)).replace(////g,"%")}))}var str = en("1哇哈哈");console.log(str.length);console.log(de(str));</script></body></html>以上所述是小編給大家介紹的JS中的進(jìn)制轉(zhuǎn)換以及作用的全部敘述,希望對(duì)大家有所幫助,如果大家想了解更多內(nèi)容,敬請(qǐng)關(guān)注武林網(wǎng)網(wǎng)站!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注