The binary conversion of js is divided into mutual conversion between binary, octal, decimal and hexadecimal. We can directly use the object.toString() to implement:
Run the following code
//Convert decimal to hexadecimal(10).toString(16) // =>"a"// Convert decimal to hexadecimal(012).toString(16) // =>"a"// Convert hexadecimal to decimal(0x16).toString(10) // =>"22"// Convert hexadecimal to english(0x16).toString(8) // =>"26"// Convert decimal to binary//=>(1111).toString(2) // => "10001010111"// Convert english to binary//=>(01111).toString(2) //=>"1001001001"//Convert hexadecimal to binary//=>(0x16).toString(2) // => "10110"
If you want to process binary to decimal, hexadecimal to decimal, octal to decimal, you need to use paresInt method:
Run the following code
//2-digit to decimal; parseInt(10,2) //=>2//2-digit to decimal; parseInt(100,2) //=>4//hexadecimal to decimal parseInt(12, 16) //=>18//8-digit to decimal parseInt(12, 8); //=>10
Priority conversion
If you want to implement conversion between the digits, you can use the parseInt method to convert it to decimal first, and then use toString (parameters) to convert it to different digits;
A tool for realizing a binary conversion using toString and parseInt methods:
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>Unt titled 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);b reak;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,16);break;case(7):to=parseInt(n um,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="Input illegal characters"document.getElementById("out").value=to;}</script><select name="title" id="title"><option value="1">Decimal to binary</option><option value="2">Decimal to octal</option><option value="3">Decimal to hex</option><option value="4">Binary to decimal</option><option value="5">Octal to decimal</option><option value="6">Hex to decimal</option><option value="7">Binary to octal</option><option value="8">Octal to binary</option><option value="9">Binary to hex</option><option value="10">Hex to binary</option><option value="10">Hex to binary</option><option value="7">Binary to octal</option><option value="8">Octal to binary</option><option value="9">Binary to hex</option><option value="10">Hex to binary</option><option value="11">Octal to hexadecimal</option><option value="12">Hex to octal</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
Convert the string to unicode, and then convert the unicode to a different binary to implement code encryption:
Run the following code
<!DOCTYPE html><html><head><meta charset="utf-8"/><title></head><body><script>function en(code, h){//Simple jS encryption and decryption<br>//code is the corresponding string, h is (2, 8, 10, 16) is the number of converted into<br>function en(code, h) {var monkey = new Array();var i;for(i=0;i<code.length;i++)monyer+=code.charCodeAt(i).toString(h)+"_";//It is to convert the string into the ascll code, and then convert it into the number of binary you want to return monkey;};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("1Wahaha",8) //=> "61_52307_52310_52310_"de("61_52307_52310_52310_52310_",8) //=> "1Wahaha</script></body></html>Zero-width characters
Using the zero width of the zero width character, we convert all strings into binary and then use zero width characters to represent them. Then the generated string length will be 0, which can be restored by decompiling.
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 characters other than Latin-1 encoding. return escape(a).replace('%', '//');});str = str.replace(/[/s/S]/g, function (a) { // Process binary data and perform 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("1Wow haha");console.log(str.length);console.log(de(str));</script></body></html>The above is the complete description of the binary conversion and function in JS introduced to you by the editor. I hope it will be helpful to you. If you want to know more, please pay attention to the Wulin.com website!