Understand hexadecimal conversion and its function in JS
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 = { '00': '\u200b', '01': '\u200c', '10': '\u200d', '11': '\uFEFF' }; str = str.replace(/[^\x00-\xff]/g, function (a) { // 转码 Latin-1 编码以外的字符。 return escape(a).replace('%', '\\'); }); str = str.replace(/[\s\S]/g, function (a) { // 处理二进制数据并且进行数据替换 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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.
