tags:

views:

103

answers:

3

How can i assign the below code to a number?

Integer.toHexString( myHexValue );

+5  A: 

The second optional argument to parseInt is the radix.

parseInt("0xff", 16);
// yields 255
John Cromartie
+1 — Also, the "0x" is optional when specifying the radix, so `x == parseInt(Integer.toHexString(x), 16)`.
Ben Blank
+1  A: 

Convert it back to an integer...

var s = "FFFFFF";
var n = parseInt("0x"+ s);
BenAlabaster
It's actually just a coincidence that "0xff" parses to 255 without a radix, because most JS implementations read the "0x" and proceed to parse the rest as hex. Unfortunately the output of most int->hex procedures omits the "0x" and would not work in this case.
John Cromartie
P.S. Sorry, I see that you're doing parseInt("0x" + s), but still I'd say that's less than optimal when you have other options.
John Cromartie
I really don't see how what I'm doing is different from what you're doing. He's assigning the hex value to a string variable, which I'm doing directly whereas in his example he's using Integer.toHexString, and then I'm doing the exact same thing you are, but not specifying the radix, which is optional anyhow. So I'm not following your point...
BenAlabaster
I think I confused the issue by adding "0x" in my original example. The original question is posed in terms of a string generated by Java's Integer::toHexString, which returns a plain hex string without the "0x".
John Cromartie
A: 

You can cast the hex value as a "Number". For example:

foo = Number(0xDEADBEEF)

Hope this helps.

cm2