Available CategoriesAdobeMacromediaProgrammingSQLServer AdministrationNetworkingMicrosoft ProductsMac OSLinux systemsMobile devicesXMLCertificationMiscAvailable Tutorials.NET Framework Essentials.NET Programming securityC# In A Nutshell TutorialProgramming C.SharpMastering Visual Studio .NETASP.NETWeb Solutions based on ASP.NET and ADO.NETJava data objectsJava extreme programmingJava performance tuningJava development on pda's. Building applications for pocket pc and palm devicesJavaScript and DHTMLLearning UMLUMLLearning XMLCocoaProgramming CppPerl objects, references and modulesPerl tutorialPython tutorialPython. Text processingPocket pc network programmingPHP & MySQL. Building web database applicationsPHP & MySQL. Programming for beginnersPHP, MySQL and Apache in 24 hoursSoftware architecture in practiceSoftware engineering and computer gamesBuilding Solutions With the Microsoft .NET Compact FrameworkProgramming Microsoft Visual C# 2005ActionscriptMastering Delphi 7Ado.netPractical mod_perlPerl for bioinformaticsWeb ServicesPrinciples of Secure CodingC/C++ Secure ProgrammingASP.NET AJAXVisual C#Borland C++ Builder 6 Developer's Guide |
2.6 Converting Between Decimal and Hexadecimal Numbers
2.6.1 ProblemYou want to change a decimal number to its hexadecimal equivalent, and vice versa. 2.6.2 SolutionWhile the core JavaScript language provides facilities for going from hexadecimal to decimal, you need a custom function to go the other way. To get a hexadecimal number as a string into its decimal equivalent, use the parseInt( ) method and specify the second parameter as 16: var decimalVal = parseInt(myHexNumberValue, 16); For myHexNumberValue, you can use either the hexadecimal characters for the number, or the format required for hexadecimal arithmetic in JavaScript: the hexadecimal characters preceded by 0x or 0X (a zero followed by an X). Here are some examples with string literals in the two formats: var decimalVal = parseInt("1f", 16);
var decimalVal = parseInt("0x1f", 16);
To convert a decimal number (between 0 and 255) to a hexadecimal string equivalent, use the following function: function dec2Hex(dec) {
dec = parseInt(dec, 10);
if (!isNaN(dec)) {
hexChars = "0123456789ABCDEF";
if (dec > 255) {
return "Out of Range";
}
var i = dec % 16;
var j = (dec - i) / 16;
result = "0x";
result += hexChars.charAt(j) + hexChars.charAt(i);
return result;
} else {
return NaN;
}
}
Because JavaScript automatically converts hexadecimal numbers to their decimal equivalents for arithmetic operations, the hexadecimal conversion is needed only for display of a hexadecimal result. 2.6.3 DiscussionHexadecimal arithmetic isn't used much in
JavaScript, but the language provides rudimentary support for base 16
numbers. As long as you signify a hexadecimal number value with the
leading
var result = 0xff - 200; Hexadecimal digits a through f may be expressed in your choice of upper- or lowercase letters. The parseInt( ) method is frequently a handy tool for getting values in other bases into decimal. For example, you obtain a decimal equivalent of a binary number string by specifying base 2 as the second argument of the method: var decimalVal = parseInt("11010011", 2);
2.6.4 See AlsoRecipe 2.1 for converting between number and string value types.
|