# [Code Example] Convert binary to hexadecimal in JavaScript & ReactJs

In this article we will provide you the code to convert a binary number to hexadecimal in Javascript & ReactJs. The process is to first convert the binary number to decimal number and then from decimal to hexadecimal. Check out the code –

function binaryToDecimal(x) {
const parsed = parseInt(x, 2);
if (isNaN(parsed)) { return 0; }
return parsed;
}

function decimalToHex(x) {
var parsed = x;

hexstr = ""
while(parsed > 0) {
var value = parsed % 16
hexstr = (
value == 10 ? "A" : (
value == 11 ? "B" : (
value == 12 ? "C" : (
value == 13 ? "D" : (
value == 14 ? "E" : (
value == 15 ? "F": value)))))) + hexstr
parsed = parseInt(parsed / 16)
}

return '0x'+ hexstr;
}

function binaryToHex(x) {
return decimalToHex(binaryToDecimal(s))
}

console.log(binaryToHex('1001'));

Get only the best articles delivered to your mail

## [Code Example] Check if string starts with substring or not using bash

In this article we will provide you bash script code example to check if substring is present at the start of the string. This code won’t check any other occurrences…

## [Code Example] valueerror: expected 2d array, got 1d array instead

In this article we will look at Python code example to resolve valueerror: expected 2d array, got 1d array instead. Some of the functions in Python expects 2d array as…

## [Code Example] How to add css to html

In this article we will provide you code to add css to html files. You can add css styles using <style> tag. To add external css file, you can use…

## [Code Example] Convert decimal to hexadecimal in javascript & reactjs

To convert a decimal number to hexadecimal in javascript and reactjs, use these steps – Run a loop till provided value become 0 Take mod by 16 and convert the…

## delete local – “git branch -d bname”; del remote -“git push origin -d bname”

In this article we will show you the commands to delete git branch locally and remotely. Local Branch Delete – For force delete – Remote Branch Delete – Local remote-tracking…

## [Code Example] Loop over contents of file and read line by line in bash

In this code example we will share a bash function to loop over contents of input file. It contains a while loop which reads the content line by line. Code…