How to convert an hex string to a Hex number

Discussion in 'Javascript' started by chirs, Dec 1, 2003.

  1. chirs

    chirs Guest

    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
     
    chirs, Dec 1, 2003
    #1
    1. Advertising

  2. (chirs) writes:

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

    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Dec 1, 2003
    #2
    1. Advertising

  3. > 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?


    Buy a book. You can't program in total ignorance. I recommend Flanagan's
    Definitive Guide (4th Ed) from O'Reilly.

    Try

    var a = 0xffff00;

    http://www.crockford.com/javascript/javascript.html
     
    Douglas Crockford, Dec 1, 2003
    #3
  4. JRS: In article <>, seen
    in news:comp.lang.javascript, chirs <> posted at Sun, 30
    Nov 2003 21:36:27 :-
    >
    >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.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
     
    Dr John Stockton, Dec 1, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,229
    Neredbojias
    Aug 19, 2005
  2. Replies:
    5
    Views:
    527
    Pete Becker
    May 21, 2005
  3. Bengt Richter
    Replies:
    6
    Views:
    474
    Juha Autero
    Aug 19, 2003
  4. Hako
    Replies:
    13
    Views:
    38,872
    Ron Adam
    Nov 10, 2005
  5. oddstray
    Replies:
    5
    Views:
    1,258
    Tim Rentsch
    Sep 15, 2004
Loading...

Share This Page