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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/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>
1

6 Answers 6

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 recommended to always pass the radix to avoid unwanted side effects):

parseInt("0x1a"); //26
Sign up to request clarification or add additional context in comments.

6 Comments

Keep in mind that the radix is arbitrary (within limits), so you can specify pretty much any base you like and convert it, e.g. base 3 to base 20.
Good answer, except the bit about "safely omiting the radix". IMO it's never safe to omit the radix. See what happens when your input starts with a zero: jsfiddle.net/dbtZj
so why it is showing 256 for var par= "100"; alert (parseInt(par,16))
@JitenderChand - Because 100 in base 16 is 256 in base 10!
@Jamiec - Agreed, you should always pass in the radix. Edited to make that clearer! Although the octal assumption when the number starts with 0 is not in the spec and will hopefully die out in the future.
|
3

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

Comments

1

looks like it's converting 1a from hexadecial to decimal

Comments

1

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.

Comments

1

You are parsing 1A to the base of 16. So this makes

1 * 16^1 + 10 * 16^0 = 16 + 10 = 26

Comments

0

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.org/wiki/Hexadecimal for more info.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.