Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to convert an hex string to a Hex number

Reply
Thread Tools

How to convert an hex string to a Hex number

 
 
chirs
Guest
Posts: n/a
 
      12-01-2003
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
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      12-01-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (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 - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
Douglas Crockford
Guest
Posts: n/a
 
      12-01-2003
> 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

 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      12-01-2003
JRS: In article <(E-Mail Removed)> , seen
in news:comp.lang.javascript, chirs <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to convert a number to hex number? Hako Python 13 11-10-2005 01:56 AM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
how to convert very large number to hex string oddstray C Programming 5 09-15-2004 12:30 AM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments