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

html - parseInt function in javascript - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

6 Answers 6

Reset to default 8

The 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.

发布评论

评论列表(0)

  1. 暂无评论