Home >Web Front-end >JS Tutorial >How to convert integer to hexadecimal in javascript
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);".
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)
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));
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);
【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!