Home >Web Front-end >JS Tutorial >How to convert numbers to Hex (hexadecimal) in javascript

How to convert numbers to Hex (hexadecimal) in javascript

青灯夜游
青灯夜游Original
2022-01-12 11:13:039523browse

In javascript, you can use the toString() method of the Number object to convert the number to Hex (hexadecimal). This method can parse the specified value and try to return a string of the specified base (base) Representation form, the syntax is "specify digital object.toString(16);".

How to convert numbers to Hex (hexadecimal) in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, you can use the toString() method of the Number object to convert the number to Hex (hexadecimal).

Example:

var num = 255;
console.log(num.toString(16));//输出FF

How to convert numbers to Hex (hexadecimal) in javascript

Description:

toString() method can convert a Number object Convert to a string and return the result.

Syntax:

NumberObject.toString(radix)
Parameters Description
radix Optional. Specifies the base in which the number is represented, making it an integer between 2 and 36. If this parameter is omitted, base 10 is used. Note, however, that the ECMAScript standard allows implementations to return any value if the parameter is a value other than 10.

# The toString() method parses its first argument and attempts to return a string representation of the specified radix.

For letters with a base greater than 10, the letters represent numbers greater than 9. For example, for hexadecimal numbers (base 16), use a through F.

If no base is specified, the preferred base is assumed to be 10.

var x='110';  
alert(parseInt(x,2));  //6    =>以2进制解析110
alert(parseInt(x,8));  //72    =>以8进制解析110
alert(parseInt(x,16)); //272    =>以16进制解析110

【Related recommendations: javascript learning tutorial

The above is the detailed content of How to convert numbers to Hex (hexadecimal) in javascript. 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