Decimal hexadecimal code in JavaScript

Source: Internet
Author: User

The core of JavaScript color conversion is the transformation between transitions. RGB format is actually decimal notation, so the hexadecimal color and RGB color conversion is hexadecimal and decimal conversion between.

16 conversion to decimal is relatively easy, the core code is as follows: parseint ("0xFF"), the result is 255, "0x" indicates that the current is 16, because the parseint after no parameters, the default is to convert to 10 into the system.

Decimal conversion to 16, the core code is as follows: Var num=255; Num.tostring (16), and the result is ff. "16″ represents a numeric conversion to a 16-in string.


Cases

The code is as follows Copy Code

var r = parseint (Math.random () *16);
var g = parseint (Math.random () *16);
var B = parseint (Math.random () *16);
Convert to 16, using int.tostring (16).
Accordingly, you can also use ToString (Ten), ToString (8), ToString (2) to convert to decimal, octal, binary, etc.
R = r.tostring (16);
g = g.tostring (16);
b = b.tostring (16);
RGB values that are spliced into colors
var color = ' # ' +r+g+b;


The system converts to hexadecimal or octal, or vice versa

Do you write a separate function to convert hexadecimal (or octal)? Stop now! There are easier ready-made functions to use:

JavaScript code copy content to clipboard

  code is as follows copy code

• (int). ToString (16); converts int to hex, eg => "C"     
 
• (int). toString (8); //converts in T to octal, eg. =>     
 
parseint (string,16)//converts hex to int, eg. "FF" => 255    
 
parseint (string,8)//converts octal to int, eg. "=>"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.