To convert a number from base 10 to base 16 in TypeScript, you can use the toString()
method with a radix of 16. Here's an example:
index.ts96 chars5 lines
In this example, num
is the base 10 number that we want to convert to base 16. We then use the toString()
method with a radix of 16 to convert it to hexadecimal. The resulting value is stored in hex
, which is a string.
You can also use a custom function to perform the conversion manually, like this:
index.ts428 chars19 lines
In this example, decToHex()
is a custom function that takes a base 10 number and returns a hexadecimal string. Inside the function, we use a lookup table (hexLookup
) to convert each digit to its hexadecimal equivalent, then we iterate through the digits of the input number and append each hexadecimal digit to the result string (result
). Finally, we return the result string, which contains the hexadecimal representation of the input number.
gistlibby LogSnag