Binary Conversion and Its Function in JS

  • 2021-07-01 06:13:22
  • OfStack

The binary conversion of js is divided into binary, octal, decimal and hexadecimal conversion, which can be realized directly by using the object. toString ():

Run the following code


//10 Binary conversion 16 Binary system 
(10).toString(16) // =>"a"
//8 Binary conversion 16 Binary system 
(012).toString(16) // =>"a"
//16 Binary conversion 10 Binary system 
(0x16).toString(10) // =>"22"
//16 Binary conversion 8 Binary system 
(0x16).toString(8) // =>"26"
//10 Binary conversion 2 Binary system  //=>
(1111).toString(2) // => "10001010111"
//8 Binary conversion 2 Binary system  //=>
(01111).toString(2) //=>"1001001001"
//16 Binary conversion 2 Binary system  //=>
(0x16).toString(2) // => "10110"

If you want to deal with binary to decimal, hexadecimal to decimal, and octal to decimal, you need to use paresInt:

Run the following code


//2 Binary to 10 Binary system; 
parseInt(10,2) //=>2
//2 Binary to 10 Binary system; 
parseInt(100,2) //=>4
//16 Binary to 10 Binary system 
parseInt(12, 16) //=>18
//8 Binary to 10 Binary system 
parseInt(12,8); //=>10

Binary conversion

If you want to realize the conversion between binary systems, you can use parseInt method to convert it into 10 binary systems first, and then use toString (parameter) to convert it into different binary systems;

Using toString and parseInt methods, one binary conversion tool can be realized:

Run the following code


<!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> Untitled document </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=" You entered illegal characters "
document.getElementById("out").value=to;
}
</script>
<select name="title" id="title" style="width:152px;">
<option value="1">10 Binary conversion 2 Binary system </option>
<option value="2">10 Binary conversion 8 Binary system </option>
<option value="3">10 Binary conversion 106 Binary system </option>
<option value="4">2 Binary conversion 10 Binary system </option>
<option value="5">8 Binary conversion 10 Binary system </option>
<option value="6">106 Binary conversion 10 Binary system </option>
<option value="7">2 Binary conversion 8 Binary system </option>
<option value="8">8 Binary conversion 2 Binary system </option>
<option value="9">2 Binary conversion 106 Binary system </option>
<option value="10">106 Binary conversion 2 Binary system </option>
<option value="11">8 Binary conversion 106 Binary system </option>
<option value="12">106 Binary conversion 8 Binary system </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;">* Note: When there are illegal characters, we only truncate valid characters for conversion </font>
</body>
</html>

Simple encryption and decryption

The string into unicode, and then unicode into a different binary, code encryption processing:

Run the following code


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
</head>
<body>
<script>
function en(code, h){
// Simple jS Encryption and decryption <br>//code Is the corresponding string, h For ( 2 , 8 , 10 , 16 ) is the binary system to be converted <br>function en(code, h) {
var monyer = new Array();var i;
for(i=0;i<code.length;i++)
monyer+=code.charCodeAt(i).toString(h)+"_";// Is to turn the string into ascll Code, and then turn it into the binary system you want 
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 Wow ha ha ",8) //=> "61_52307_52310_52310_"
de("61_52307_52310_52310_",8) //=> "1 Wow ha ha 
</script>
</body>
</html>

Zero width character

Using the zero width of the zero-width character, we convert all strings to binary, and then use the zero-width character to represent them, so the generated string will be 0 in length, which can be restored by decompilation.

Run the following code


<!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) { //  Transcoding  Latin-1  Characters other than encoding. 
return escape(a).replace('%', '\\');
});
str = str.replace(/[\s\S]/g, function (a) { //  Deal with 2 Binary data and data replacement 
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 Wow ha ha ");
console.log(str.length);
console.log(de(str));
</script>
</body>
</html>

Above is the site to introduce you to the JS in the binary conversion and the role of all narration, I hope to help you, if you want to know more content, please pay attention to this site website!


Related articles: