Comments on Convert to Hexadecimal
Parent
Convert to Hexadecimal
+3
−1
Challenge
Write a program that takes in a number greater than or equal to 0 and outputs its representation in hexadecimal (base 16).
Examples
0 => 0
1 => 1
10 => A
15 => F
16 => 10
107 => 6B
153 => 99
207 => CF
1000 => 3E8
10381 => 288D
48821 => BEB5
Rules
- No built-in hexadecimal functions.
- More generally, any base conversion function is considered to be disallowed.
- Letters can be uppercase or lowercase.
- It should work at least for all inputs greater than 0 up to the language's default type limit.
- Leading zeros in the output is fine. (i.e.
000003E8for 32-bit numbers) - This is [code-golf], so shortest code wins.
+5
−0
Vyxal, 24 bytes ``` 16ʀẋf …
1y ago
+3
−0
Vyxal, 15 bytes ``` ‡16ḭ↲ …
9mo ago
+2
−0
Vyxal 3, 15 13 bytes ``` ⑴ …
9mo ago
+2
−0
Lean 4, 103 100 95 83 bytes …
1y ago
+1
−0
[Python 3.8 (pre-release)], 70 …
12mo ago
+1
−0
[JavaScript (Node.js)], 60 byt …
1y ago
+0
−0
R, 73 bytes ```r `-`=\(n,s …
8mo ago
Post
+1
−0
Python 3.8 (pre-release), 70 bytes
-1 bytes using default arg & -1 bytes using >>= (thanks trichoplax)
def h(n,a=''):
while n>0:a="0123456789ABCDEF"[n%16]+a;n>>=4
return a

3 comment threads