Function: hexToSigned()
hexToSigned(
hex):number
Read a hexadecimal string as a signed integer in two's complement, its width given by its number of digits (4 bits each): 'FF' is the 8-bit -1, '00FF' the 16-bit 255, '8000' the 16-bit -32768. Pad the string with zeros to the width of the value read.
Parameters
hex
string
The hexadecimal digits, in either case. A 0x/0X prefix is accepted and does not change the width ('0x00FF' gives 255, as '00FF' does).
Returns
number
The signed value.
Throws
When hex has no digit ('', '0x') or a character which is not a hexadecimal digit (a sign, a space, '0x1g').
Throws
When the value is beyond Number.MAX_SAFE_INTEGER in magnitude: it would lose its precision as a number.
Example
typescript
import {hexToSigned} from 'lakutata/utils'
console.log(hexToSigned('7F'), hexToSigned('80'), hexToSigned('FFFF'), hexToSigned('00FF'), hexToSigned('0x00FF'))
//127 -128 -1 255 255