How to convert an hex string to a Hex number

C

chirs

Hi,

I'd like to use a var to hold a Hex number. But in this code:

var a = "ffff00"

a is a string. var a=ffff00 will not work either. How can I put a
Hex number ffff00 into a var?

Thanks

Chris
 
L

Lasse Reichstein Nielsen

I'd like to use a var to hold a Hex number. But in this code:

var a = "ffff00"

a is a string. var a=ffff00 will not work either. How can I put a
Hex number ffff00 into a var?

var a = 0xffff00;

In Javascript, and most other languages with a C like syntax,
hexadecimal numbers are written with a prefix "0x".
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
I'd like to use a var to hold a Hex number.

Variables hold IEEE Doubles as the only form of number, except maybe in
future. You can, however, use Hex notation to specify the contents of a
variable.
But in this code:

var a = "ffff00"

a is a string. var a=ffff00 will not work either. How can I put a
Hex number ffff00 into a var?

var a = 0xffff00 // 1. to give a literal in Hex

var HS = "ffff00"

var a = +("0x"+HS) // 2. using existing string

var a = parseInt(HS, 16) // 3. normal, using existing string

Here 2 & 3 give different result with different illegal strings HS; try
"fated" & "defeat"; I prefer the former.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top