JavaScript tips — Convert strings of hex digits into numbers

preview_player
Показать описание
Convert strings of hex digits into numbers using #JavaScript's built-in parseInt function

For basic uses you can simply call parseInt with a string that starts with '0x' followed the hex digits. parseInt('0xA3C') for example returns the number 2620

If you want to omit the '0x' prefix from the string or enforce that the string is always interpreted as hex, call parse in with 16 as its second argument: parseInt('A3C', 16). The second argument is the radix (base) used when parsing the number

Keep in mind that parseInt returns a JavaScript number, so it has precision limitations when dealing with very large numbers. For these cases, you can use BinInt instead: BigInt('0xffffffffffffff')

Рекомендации по теме