@chriscodesthings/is-hex-number
v1.3.0
Published
Determine if a string can be evaluated as a hexadecimal number
Downloads
3
Maintainers
Readme
is-hex-number
Function to determine if a string is a hexadecimal number
Description
Tests a variable to see if it's a hex number.
See...
Install
npm install --save @chriscodesthings/is-hex-number
Use
import isHexNumber from '@chriscodesthings/is-hex-number';
console.log(isHexNumber("c0ffee"));
// => true
Syntax
isHexNumber(str, float, prefix0x);
Parameters
- str: a string to test
- float (optional): if
true
, allow a floating point hex number - prefix0x (optional): if
true
, allow the 0x prefix
Return Value
Returns boolean true
if str is a valid hex number, false
otherwise.
Examples
// convert to decimal or return null if invalid
function hex2dec(hex) {
if( isHexNumber(hex)) {
return parseInt(hex,16);
}
return null;
}