@chriscodesthings/is-hex-number
TypeScript icon, indicating that this package has built-in type declarations

1.3.0 • Public • Published

is-hex-number
Test workflow status NPM Version License: MIT

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

See Also...

Readme

Keywords

Package Sidebar

Install

npm i @chriscodesthings/is-hex-number

Weekly Downloads

2

Version

1.3.0

License

MIT

Unpacked Size

7.41 kB

Total Files

8

Last publish

Collaborators

  • chriscodesthings