"a"//convert hexadecimal It is decimal (0x16).toString(10) // =>"22"//Convert hexadecimal to octal"/> "a"//convert hexadecimal It is decimal (0x16).toString(10) // =>"22"//Convert hexadecimal to octal">

Home  >  Article  >  Web Front-end  >  Understand hexadecimal conversion and its function in JS

Understand hexadecimal conversion and its function in JS

怪我咯
怪我咯Original
2017-04-01 09:26:591385browse

js的进制转换, 分为2进制,8进制,10进制,16进制之间的相互转换, 我们直接利用 对象.toString()即可实现:

运行下面代码

//10进制转为16进制
(10).toString(16) // =>"a"
//8进制转为16进制
(012).toString(16) // =>"a"
//16进制转为10进制
(0x16).toString(10) // =>"22"
//16进制转为8进制
(0x16).toString(8) // =>"26"
//10进制转为2进制 //=>
(1111).toString(2) // => "10001010111"
//8进制转为2进制 //=>
(01111).toString(2) //=>"1001001001"
//16进制转为2进制 //=>
(0x16).toString(2) // => "10110"

  如果要处理2进制到10进制,16进制到10进制,8进制到10进制, 需要用了paresInt这个方法:
运行下面代码

//2进制到10进制;
parseInt(10,2) //=>2
//2进制到10进制;
parseInt(100,2) //=>4
//16进制到10进制
parseInt(12, 16) //=>18
//8进制到10进制
parseInt(12,8); //=>10

  进制转换
  如果要实现进制之间的转换, 可以利用parseInt方法, 先转化为10进制, 然后再利用toString(参数), 转化成不同的进制;
  利用toString和parseInt方法可以实现一个进制转化的工具:
运行下面代码

<!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>无标题文档</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">十进制转二进制</option>
<option value="2">十进制转八进制</option>
<option value="3">十进制转十六进制</option>
<option value="4">二进制转十进制</option>
<option value="5">八进制转十进制</option>
<option value="6">十六进制转十进制</option>
<option value="7">二进制转八进制</option>
<option value="8">八进制转二进制</option>
<option value="9">二进制转十六进制</option>
<option value="10">十六进制转二进制</option>
<option value="11">八进制转十六进制</option>
<option value="12">十六进制转八进制</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;">*注:存在非法字符时,我们只截断有效字符进行转换</font>
</body>
</html>

  简单加密解密
  把字符串转化成unicode, 然后再把unicode转成不同的进制 , 实现代码加密处理:
运行下面代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
</head>
<body>
<script>
function en(code, h){
//简单的jS加密解密<br>//code为对应的字符串,h为(2,8,10,16)就是要转成的几进制<br>function en(code, h) {
var monyer = new Array();var i;
for(i=0;i<code.length;i++)
monyer+=code.charCodeAt(i).toString(h)+"_";//就是把字符串转成ascll码,然后再转成你想的几进制
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>

  零宽字符

  利用零宽字符的零宽度, 我们把所有的字符串转化成二进制, 然后利用零宽字符进行表示, 那么生成的字符串长度就会为0, 主要反编译即可还原,

运行下面代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
</head>
<body>
<script>
function en(str) {
var rep = {
&#39;00&#39;: &#39;\u200b&#39;,
&#39;01&#39;: &#39;\u200c&#39;,
&#39;10&#39;: &#39;\u200d&#39;,
&#39;11&#39;: &#39;\uFEFF&#39;
};
str = str.replace(/[^\x00-\xff]/g, function (a) { // 转码 Latin-1 编码以外的字符。
return escape(a).replace(&#39;%&#39;, &#39;\\&#39;);
});
str = str.replace(/[\s\S]/g, function (a) { // 处理二进制数据并且进行数据替换
a = a.charCodeAt().toString(2);
a = a.length < 8 ? Array(9 - a.length).join(&#39;0&#39;) + 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

The above is the detailed content of Understand hexadecimal conversion and its function in JS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn