Number.prototype.toString(radix) chuyển số sang string cơ số radix (2–36).
Decimal → Hex
console.log(Number(255).toString(16)); // "ff" console.log(Number(17).toString(16)); // "11" console.log((255).toString(16)); // "ff" (literal cũng được)
Muốn uppercase: .toString(16).toUpperCase() → "FF".
Hex → Decimal: parseInt(string, 16)
console.log(parseInt("ff", 16)); // 255
console.log(parseInt("11", 16)); // 17
Các cơ số khác
- Binary (base 2):
num.toString(2),parseInt(str, 2) - Octal (base 8):
num.toString(8),parseInt(str, 8) - Base 36 (0-9, a-z):
num.toString(36)