# Tip: Convert decimal number to hexadecimal

JavaScript, Math · Sep 21, 2022 Numeric values are represented in decimal format by default, when converted to strings. If you want to display them in hexadecimal format, you can use `Number.prototype.toString()` and pass the base you want to use (`16`) as an argument.

```const decimalToHex = dec => dec.toString(16);

decimalToHex(0); // '0'
decimalToHex(255); // 'ff'```

Conversely, the opposite might also be needed. You can use `parseInt()` to convert a string to a number in a given base. If you don't specify a base, it will default to `10`.

```const hexToDecimal = hex => parseInt(hex, 16);

hexToDecimal('0'); // 0
hexToDecimal('ff'); // 255```

### Written by Angelos Chalaris

I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.

If you want to keep in touch, follow me on GitHub or Twitter.

## More like this

• ### Tips & Tricks

A collection of quick tips and tricks to level up your coding skills one step at a time.

Collection · 53 snippets

• ### Number to decimal mark

Converts a number to a decimal mark formatted string.

JavaScript, Math · Oct 22, 2020

• ### Number to fixed-point notation without trailing zeros

Formats a number using fixed-point notation, if it has decimals.

JavaScript, Math · May 10, 2022

• ### Number has decimal digits

Checks if a number has any decimals digits

JavaScript, Math · May 13, 2022