Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

The JavaScript FAQ
Prev Home Next

Octals and Hexadecimals

Question: Is there a way to use octal and hexadecimal numbers in JavaScript?

Answer: Yes. In JavaScript you can use octals and hexadecimals.

The following are examples of octal numbers:
01234 -077 0312
Positive octal numbers must begin with 0 (zero) and negative octal numbers must begin with -0.

And these are examples of hexadecimal numbers:
0xFF -0xCCFF 0xabcdef
Positive hexadecimals must begin with 0x and negative hexadecimals must begin with -0x.

When you need to convert an octal or hexadecimal string to a number, use the function parseInt(str,base). Consider these examples:

octalStr='377';
num = parseInt(octalStr,8);  // num now holds 255

hexStr='7F';
num = parseInt(hexStr,16);   // num now holds 127
The second argument of parseInt specifies the base of the number whose representation is contained in the original string. This argument can be any integer from 2 to 36.

JavaScripter.net. Copyright © 1999-2006, Alexei Kourbatov

The JavaScript FAQ
Prev Home Next


 
 
  Mirrored with kind permission of Alexei Kourbatov Design by Interspire