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

To convert a decimal number to hexadecimal in javascript and reactjs, use these steps –

1. Run a loop till provided value become 0
2. Take mod by 16 and convert the number into hexcode (0-F).
3. Set the value as value divided by 16

Code Example –

``````function decimalToHex(x) {
var parsed = parseInt(x, 10);
if (isNaN(parsed)) { return 0; }

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;
}

console.log(decimalToHex(' 245'));
// expected output: "0xF5"``````

Source: MSDN

### Sign Up for Our Newsletters

Get only the best articles delivered to your mail

## [Code Example] Getting input from terminal using bash script

In this article we will provide you bash script code for getting input from terminal. We will use read command to read the value provided by user. Code Example –…

## [Code Example] Center align IFrame

To center align an iframe using html and css, you can set text-align to center or can use flex properties. Otherwise you can also set margin auto to the parent…

## [Code Example] Get content between two substrings using bash script

To get the content between two substrings, we will provide you a bash script code. You will need to provide a file from which you want to extract the content…

## [Code Example] Count total files or directories using bash script

In this article we will look at the bash script code which can count total files or directories inside any directory. This works by passing the output of the glob…

## [Code Example] How to loop over an array using bash script

Get the code to loop over an array using bash script. We will provide 3 ways to loop over an array. You can fetch each element of array or use…

## [Code Example] Convert string into float in javascript & ReactJs

Use parseFloat() function to covert string into float in javascript & ReactJs. The digit before decimal must start with a digit otherwise it will return NaN. Check the code example…