Sling Academy
Home/JavaScript/Converting Between Number Systems (Binary, Hex) in JavaScript

Converting Between Number Systems (Binary, Hex) in JavaScript

Last updated: December 12, 2024

In computer science, number systems are a fundamental concept that every programmer should understand. Two of the most common number systems used are binary and hexadecimal. Converting between these number systems is a frequent task, especially when dealing with low-level data or applications involved in graphics and networking.

This article will guide you through converting numbers between binary, hexadecimal, and decimal using JavaScript. JavaScript provides several built-in methods to facilitate these conversions, making tasks simpler and faster.

Understanding Number Systems

Before diving into conversions, let’s briefly discuss the different number systems:

  • Binary: A base-2 numeral system. Each digit is a bit (`0` or `1`).
  • Decimal: A base-10 numeral system, usual numbers we use every day (`0` to `9`).
  • Hexadecimal: A base-16 system. It uses sixteen distinct symbols: `0`-`9` and `A`-`F`.

Converting Binary to Decimal

To convert a binary number to a decimal number in JavaScript, use the parseInt() function. It takes two arguments: the string to convert, and the base of the number in that string, which is 2 for binary numbers.

const binary = "1011";
const decimal = parseInt(binary, 2);
console.log(decimal); // Output: 11

Converting Decimal to Binary

JavaScript’s toString() method can be used to convert decimal numbers to binary. By passing 2 as an argument to the toString() method, it outputs the binary equivalent of a number.

const decimal = 11;
const binary = decimal.toString(2);
console.log(binary); // Output: "1011"

Converting Decimal to Hexadecimal

Similarly, you can convert decimal numbers to hexadecimal by using the toString() method, but this time with base 16.

const decimal = 255;
const hex = decimal.toString(16);
console.log(hex); // Output: "ff"

Converting Hexadecimal to Decimal

Again, use the parseInt() function. You provide the string and specify base 16.

const hex = "ff";
const decimal = parseInt(hex, 16);
console.log(decimal); // Output: 255

Converting Binary to Hexadecimal

To convert a binary number to hexadecimal, you first convert the binary to a decimal, and then convert that decimal to hexadecimal:

const binary = "101110";
const decimal = parseInt(binary, 2);
const hex = decimal.toString(16);
console.log(hex); // Output: "2e"

Converting Hexadecimal to Binary

Similarly, convert the hexadecimal to decimal first, then from decimal to binary:

const hex = "2e";
const decimal = parseInt(hex, 16);
const binary = decimal.toString(2);
console.log(binary); // Output: "101110"

Practical Applications

These conversions find their applications in numerous areas such as computer graphics, network programming, and more. For example, color codes in web design are often represented in hexadecimal. IPv6 is based on 128-bit binary numbers that are typically presented using hexadecimal format.

Conclusion

Mastering number system conversions in JavaScript broadens your understanding of low-level programming concepts and enhances your ability to manipulate data across different systems. With built-in methods like parseInt() and toString(), JavaScript makes these tasks relatively straightforward. Remember, the key aspects are identifying the correct bases during conversion and having an understanding of each numeral system.

Next Article: Rounding Numbers to Specific Decimals with JavaScript Math Functions

Previous Article: Implementing Basic Statistical Aggregations (Sum, Mean) in JavaScript

Series: JavaScript Numbers

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration