最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does .toString(16) convert rgb, decimal, or other inputs into a hexidecimal - Stack Overflow

programmeradmin2浏览0评论

I've tried searching everywhere I could to find the answer as to why .toString(16) converts a number to a hexidecimal value. My first question is, why 16? My second question is, how can this return letters even though I see no letters going into the code. For example, I don't understand how the following code returns ff instead of a number.

var r = 255;
r.toString(16); //returns ff

I've tried searching everywhere I could to find the answer as to why .toString(16) converts a number to a hexidecimal value. My first question is, why 16? My second question is, how can this return letters even though I see no letters going into the code. For example, I don't understand how the following code returns ff instead of a number.

var r = 255;
r.toString(16); //returns ff

If anyone has any links or insights as to why this is, please let me know. I'm very curious. Thank you in advance!

Share Improve this question asked Sep 23, 2016 at 2:12 atrevatrev 911 gold badge2 silver badges6 bronze badges 2
  • 2 Because that is what it's meant to do. Here. – Soubhik Mondal Commented Sep 23, 2016 at 2:14
  • Because colors hex like #ffffff comes from the RGB values combined to issue a color. so 255,255,255 will create ffffff combined. – Adam Azad Commented Sep 23, 2016 at 2:18
Add a comment  | 

4 Answers 4

Reset to default 15

Hexadecimal is base 16. Break down the words: hexa, meaning 6; decimal, meaning 10. 10 + 6 = 16. A few major bases are:

  • Base 2: Binary, 2 numbers: [0, 1]
  • Base 8: Octal, 8 numbers: [0, 7]
  • Base 10: Decimal, 10 numbers: [0, 9]
  • Base 16: Hexadecimal, 16 symbols: [0, 9] and [A, F]

Per the MDN documentation:

For Number objects, the toString() method returns a string representation of the object in the specified radix.

Parameters

radix: Optional. An integer between 2 and 36 specifying the base to use for representing numeric values.

This means it converts the number into a string, and based on the radix. The syntax for Number.prototype.toString is:

number.toString([radix])

Where radix is optional. If you specify the radix, it will convert with that base, so 16 is hexadecimal. If radix is not specified, 10 (decimal) is assumed. Here's a snippet:

var num = 16;

console.log(num.toString()) //"16", base 10 is assumed here if no radix given
console.log(num.toString(16)) //"10", base 16 is given

Now, regarding RGB values: take (255, 255, 255) [white] as an example. Each individual value (red, green, or blue) is represented in hex. Since 255 is 0xFF or simply FF in hex, the full representation is FFFFFF, or ffffff you see.

This is happening because you are using 16 as the radix, as explained here:

http://www.w3schools.com/jsref/jsref_tostring_number.asp

If you are just trying to get back "16"you can just do:

var number = 16;
var numberAsString = number.toString();

In returns a 'String' since toString() is set to return a String.

In addition, it is toString(16) because hexadecimal means 16 and it is base 16. Therefore, toString(16) converts a given variable into a String in a desired form, in this case you want it to be in the hexadecimal form.

http://www.w3schools.com/jsref/jsref_tostring_number.asp

Parameter: radix

Description: Optional. Which base to use for representing a numeric value. Must be an integer between 2 and 36.

  • 2 - The number will show as a binary value
  • 8 - The number will show as an octal value
  • 16 - The number will show as an hexadecimal value

Example Convert a number to a string, using different bases:

var num = 15;
var a = num.toString();
var b = num.toString(2);
var c = num.toString(8);
var d = num.toString(16);

The result of a,b,c, and d will be:

15
1111
17
f

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论