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

js hexadecimal conversion example

小云云
小云云Original
2018-03-08 15:06:202293browse

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)

2. Convert from decimal to arbitrary base

a= ,
a.toString(16) a.toString(8) a.toString(2) a.toString(32)

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("错误");       }   
}

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!

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