Bug 24156

Summary: Unary + conversion to number fails for negative hexadecimal (-0xff) 0x
Product: WebKit Reporter: Jorge <jorgechamorro>
Component: JavaScriptCoreAssignee: Cameron Zwarich (cpst) <zwarich>
Status: RESOLVED INVALID    
Severity: Normal CC: barraclough, jorgechamorro, zwarich
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: All   
OS: All   
URL: http://groups.google.com/group/comp.lang.javascript/msg/8f8fcfdc42ee3960

Description Jorge 2009-02-25 01:47:21 PST
See: 
javascript:s= "0x12";alert((+s)+ ","+ parseInt(s,16)); //-> 18,18 
but: 
javascript:s= "-0x12";alert((+s)+ ","+ parseInt(s,16)); //-> NaN,-18
Comment 1 Cameron Zwarich (cpst) 2009-02-25 23:22:11 PST
This works as expected. See section 9.3.1 of the ECMA spec:

http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

The ToNumber operation (which is used by the unary plus operator) only handles negative signs for decimal numbers, not hex or octal. It might be a bit strange, but it's the spec.
Comment 2 Jorge 2009-02-26 01:03:21 PST
(In reply to comment #1)
>
> The ToNumber operation (which is used by the unary plus operator) only handles
> negative signs for decimal numbers, not hex or octal. It might be a bit
> strange, but it's the spec.

FF, Opera and Chrome handle them fine, and that might be perceived as "better". Better than the spec -if you want- but better, after all. Now, what's the best browser ? Not Safari when it comes to hex string conversion to number... IMO, in this case better is better than the spec. Safari is doing it to/in-spec, but worse.
Comment 3 Cameron Zwarich (cpst) 2009-02-26 07:50:25 PST
If it's a potential compatibility issue down the road, then it probably makes sense to match Firefox on this.
Comment 4 Gavin Barraclough 2011-09-07 00:54:05 PDT
This was a bug in other browsers, and fixed in the latest FireFox.  Behaves correctly.