by Sai gowtham

Converting (binary,hex,octal) to decimal numbers in JavaScript

In this tutorial, we are going to learn about how to convert binary, hexadecimal and octal to decimal numbers in JavaScript.

parseInt

In JavaScript, we have the parseInt() method by using that we can make conversions.

The parseInt( ) method takes the two arguments, the first argument is string and the second argument is base by using that it returns the decimal number of a specified base.

Let’s see the conversions with the help of examples.

Converting binary to decimal

To convert a binary to decimal we need to pass base 2 as a second argument to the parseInt method.


const binaryNumber = "1100";

console.log(parseInt(binaryNumber,2)); // base 2

//output--> 12

Converting hexadecimal to decimal

To convert a hexadecimal to decimal we need to pass base 16 as a second argument to the parseInt method.


const hex = "c";

console.log(parseInt(hex,16));  //hex base-16

//output --> 12

Converting octal to decimal

To convert a octal to decimal we need to pass base 8 as a second argument to the parseInt method.

const octal = "14";

console.log(parseInt(octal,8));  //octal base-8

//output --> 12

You can also learn, how to convert decimal to (binary, hexadecimal, octal) numbers in JavaScript.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2021 (Beginner + Advanced)
JavaScript - The Complete Guide 2021 (Beginner + Advanced)
64,026 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
152,857 students enrolled
48.5 hours of video content
View Course