Home > Web Front-end > JS Tutorial > js hexadecimal conversion example

js hexadecimal conversion example

小云云
Release: 2018-03-08 15:06:20
Original
2256 people have browsed it

Base conversion is also a very important part that needs to be mastered. This article mainly shares with you examples of js base conversion, hoping to help everyone.

1. Convert from arbitrary base to decimal

parseInt("",16)  parseInt("",8) parseInt("",2)
Copy after login

2. Convert from decimal to arbitrary base

a= ,
a.toString(16) a.toString(8) a.toString(2) a.toString(32)
Copy after login

3. Convert numbers less than 65535 to 4-digit hexadecimal. Bits are padded with zeros.

function decToHex(num)
{       var newNum = num.toString(16);       
if(num<16){        return "000"+newNum;       }       
if(num<255){        return "00"+newNum;       }      
if(num<4095){        return "0"+newNum;       }       
if(num<65535){        return newNum;       }else
{        alert("错误");       }   
}
Copy after login

Related recommendations:

Understand the base conversion and its function in JS

About string and multi-base conversion in PHP Example code of function

Use JavaScript for hexadecimal conversion

The above is the detailed content of js hexadecimal conversion example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template