Skip to content

Function: unsignedToHex() ​

unsignedToHex(unsignedNumber, bits?): string

Write a non-negative integer as hexadecimal, uppercase, padded with zeros to bits / 4 digits, without prefix (255 on 16 bits is '00FF'). The reverse of hexToUnsigned.

Parameters ​

unsignedNumber ​

number

The integer, from 0 to 2^bits - 1. Beyond Number.MAX_SAFE_INTEGER the number has already lost its precision.

bits? ​

8 | 16 | 32 | 64 | 128

The width in bits: 8, 16, 32 (the default), 64 or 128.

Returns ​

string

The uppercase hexadecimal digits, exactly bits / 4 of them.

Throws ​

When unsignedNumber is not an integer (a fraction, NaN, Infinity), is negative, or does not fit in the width (256 on 8 bits).

Example ​

typescript
import {unsignedToHex} from 'lakutata/utils'

console.log(unsignedToHex(255, 8), unsignedToHex(255, 16), unsignedToHex(48879))
//FF 00FF 0000BEEF

Released under the MIT License.