Skip to content

Instantly share code, notes, and snippets.

@agirorn
Created November 23, 2016 16:04
Show Gist options
  • Save agirorn/0e740d012b620968225de58859ccef5c to your computer and use it in GitHub Desktop.
Save agirorn/0e740d012b620968225de58859ccef5c to your computer and use it in GitHub Desktop.
Convert decimal to hex in JavaScript.
function dec2hexString(dec) {
return '0x' + (dec+0x10000).toString(16).substr(-4).toUpperCase();
}
@MairwunNx
Copy link

I think it solution better

intToHex(integer) {
      let number = (+d).toString(16).toUpperCase()
      if( (number.length % 2) > 0 ) { number= "0" + number }
      return number
}

@nullhook
Copy link

I believe you can do something like this as well:
(n).toString(16).padStart(6, '0').toUpperCase()

@kenshin1102
Copy link

kenshin1102 commented Jan 20, 2021

export const numberToHex = number => {
  const HEX = 16;
  return Number(number).toString(HEX).toUpperCase()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment