Function to determine if a string is a hexadecimal number
Tests a variable to see if it's a hex number.
npm install --save @chriscodesthings/is-hex-number
import isHexNumber from '@chriscodesthings/is-hex-number';
console.log(isHexNumber("c0ffee"));
// => true
isHexNumber(str, float, prefix0x);
- str: a string to test
-
float (optional): if
true
, allow a floating point hex number -
prefix0x (optional): if
true
, allow the 0x prefix
Returns boolean true
if str is a valid hex number, false
otherwise.
// convert to decimal or return null if invalid
function hex2dec(hex) {
if( isHexNumber(hex)) {
return parseInt(hex,16);
}
return null;
}