Home > Web Front-end > JS Tutorial > How to convert integer to hexadecimal in javascript

How to convert integer to hexadecimal in javascript

青灯夜游
Release: 2022-01-18 16:36:32
Original
10828 people have browsed it

In javascript, you can use the toString() method to convert an integer to hexadecimal. This method can parse the specified value and return the string representation of the specified hexadecimal. The specific conversion syntax is "specified number" Object.toString(16);".

How to convert integer to 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 to convert integers to hexadecimal.

The toString() method can parse the specified value and try to return the string representation of the specified base (base).

Syntax:

NumberObject.toString(radix)
Copy after login

Parameter 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: 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.

Example:

var num = 255;
console.log(num.toString(16));
Copy after login

How to convert integer to hexadecimal in javascript

Take it a step further: Convert red, green and blue integer byte values ​​to hexadecimal strings

var r = 0;
var g = 255;
var b = 255;
function convert(integer) {
	var str = Number(integer).toString(16);
	return str.length == 1 ? "0" + str : str;
};

function to_rgb(r, g, b) { return "#" + convert(r) + convert(g) + convert(b); }

var color = to_rgb(r, g, b);
console.log(color);
Copy after login

How to convert integer to hexadecimal in javascript

【Related recommendations: javascript learning tutorial

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