admin管理员组文章数量:1355019
When I am running this function the value is showing up as 26. I want to know what calculation the system uses and why it is evaluating to 26.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>parseint</title>
<script type="text/javascript">
var par= "1a";
alert (parseInt(par,16))
</script>
</head>
<body>
</body>
</html>
When I am running this function the value is showing up as 26. I want to know what calculation the system uses and why it is evaluating to 26.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>parseint</title>
<script type="text/javascript">
var par= "1a";
alert (parseInt(par,16))
</script>
</head>
<body>
</body>
</html>
Share
Improve this question
edited Feb 8, 2012 at 14:29
James Tomasino
3,5811 gold badge23 silver badges38 bronze badges
asked Feb 8, 2012 at 9:55
Jitender ChandJitender Chand
932 gold badges3 silver badges13 bronze badges
1
- en.wikipedia/wiki/Hexadecimal – Tomasz Nurkiewicz Commented Feb 8, 2012 at 9:57
6 Answers
Reset to default 8The second argument of parseInt
is the radix. It shows what base to use. Base 16 is hexadecimal, and 1a
in base 16 is 26
in base 10 (decimal).
This is why it's important to always specify the radix when using parseInt
. In this case, not supplying the radix will result in the value 1
, because it attempts to parse the number in base 10, gets to the character a
, gives up and returns what it has found so far:
parseInt("1a"); //1
parseInt("1a", 10); //1 (same as above)
parseInt("1a", 16); //26
However, if the number begins with the characters 0x
or 0X
, it is assumed to be a hexadecimal number, and you can omit the radix (although it's remended to always pass the radix to avoid unwanted side effects):
parseInt("0x1a"); //26
The second parameter of parseInt
is the radix, you have specified that 1a
is a representation of an integer in base 16 (or Hexadecimal).
A quick look at any old hexadecimal to decimal lookup table shows that hex 1a
is indeed equivalent to decimal 26
looks like it's converting 1a from hexadecial to decimal
It is just basic base 16 (which you requested explicitly).
The 16s column has a 1 in it (so that is 16) and the 1s column has an a in it (so that is 10). 10+16 is 26.
You are parsing 1A to the base of 16. So this makes
1 * 16^1 + 10 * 16^0 = 16 + 10 = 26
1a is hexidecimal for 26. You often encounter values like "1a" or "ff" in HTML colors. "ff" is 16x16 or 256 and is the highest two-digit hexidecimal value.
See http://en.wikipedia/wiki/Hexadecimal for more info.
本文标签: htmlparseInt function in javascriptStack Overflow
版权声明:本文标题:html - parseInt function in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743962486a2569284.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论