Hexadecimal Conversion
Convert between hexadecimal (base-16) and other bases. Hex uses 0-9 and A-F. Commonly used in computing for compact binary representation. Each hex digit = 4 bits.
Visualization
Interactive visualization for Hexadecimal Conversion
Interactive visualization with step-by-step execution
Implementation
1class HexConverter {
2 private hexDigits = '0123456789ABCDEF';
3
4 decimalToHex(decimal: number): string {
5 if (decimal === 0) return '0';
6 if (decimal < 0) return '-' + this.decimalToHex(-decimal);
7
8 let hex = '';
9 while (decimal > 0) {
10 hex = this.hexDigits[decimal % 16] + hex;
11 decimal = Math.floor(decimal / 16);
12 }
13 return hex;
14 }
15
16 hexToDecimal(hex: string): number {
17 hex = hex.toUpperCase();
18 let decimal = 0;
19 let power = 0;
20
21 for (let i = hex.length - 1; i >= 0; i--) {
22 const digit = this.hexDigits.indexOf(hex[i]);
23 if (digit === -1) throw new Error('Invalid hex digit');
24 decimal += digit * Math.pow(16, power);
25 power++;
26 }
27
28 return decimal;
29 }
30
31 binaryToHex(binary: string): string {
32 // Pad to multiple of 4
33 while (binary.length % 4 !== 0) {
34 binary = '0' + binary;
35 }
36
37 let hex = '';
38 for (let i = 0; i < binary.length; i += 4) {
39 const chunk = binary.substr(i, 4);
40 const decimal = parseInt(chunk, 2);
41 hex += this.hexDigits[decimal];
42 }
43
44 return hex;
45 }
46
47 hexToBinary(hex: string): string {
48 return hex.split('').map(digit => {
49 const decimal = this.hexDigits.indexOf(digit.toUpperCase());
50 return decimal.toString(2).padStart(4, '0');
51 }).join('');
52 }
53
54 // RGB color conversion
55 rgbToHex(r: number, g: number, b: number): string {
56 return '#' +
57 this.decimalToHex(r).padStart(2, '0') +
58 this.decimalToHex(g).padStart(2, '0') +
59 this.decimalToHex(b).padStart(2, '0');
60 }
61
62 hexToRgb(hex: string): { r: number; g: number; b: number } {
63 hex = hex.replace('#', '');
64 return {
65 r: this.hexToDecimal(hex.substr(0, 2)),
66 g: this.hexToDecimal(hex.substr(2, 2)),
67 b: this.hexToDecimal(hex.substr(4, 2))
68 };
69 }
70}Deep Dive
Theoretical Foundation
Hexadecimal uses 16 symbols: 0-9, A-F (10-15). Each hex digit represents 4 bits. Conversion via decimal or direct bit mapping. Example: FF₁₆ = 255₁₀ = 11111111₂. Common in memory addresses, color codes, debugging.
Complexity
Time
O(log n)
O(log n)
O(log n)
Space
O(log n)
Applications
Industry Use
Memory addresses in debugging
Color codes in web design (#FF0000)
MAC addresses in networking
Assembly language programming
Cryptographic hash representations
File format specifications
Hardware register programming
Low-level system programming
Use Cases
Related Algorithms
GCD (Euclidean Algorithm)
Compute the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm. Dating back to around 300 BC and appearing in Euclid's Elements, it's one of the oldest algorithms still in common use. The algorithm is based on the principle that GCD(a,b) = GCD(b, a mod b) and is remarkably efficient with O(log min(a,b)) time complexity. The GCD is the largest positive integer that divides both numbers without remainder.
LCM (Least Common Multiple)
Calculate the Least Common Multiple (LCM) of two integers - the smallest positive integer that is divisible by both numbers. The LCM is intimately related to the GCD through the formula: LCM(a,b) = |a×b| / GCD(a,b). This relationship allows us to compute LCM efficiently using the Euclidean algorithm for GCD, achieving O(log min(a,b)) time complexity instead of naive factorization methods.
Sieve of Eratosthenes
Ancient and highly efficient algorithm to find all prime numbers up to a given limit n. Invented by Greek mathematician Eratosthenes of Cyrene (276-194 BC), this sieve method systematically eliminates multiples of primes, leaving only primes in the array. With O(n log log n) time complexity, it remains one of the most practical algorithms for generating large lists of primes, vastly superior to trial division which runs in O(n² / log n) time.
Prime Factorization
Decompose a positive integer into its unique prime factor representation. Every integer greater than 1 can be expressed as a product of prime numbers in exactly one way (Fundamental Theorem of Arithmetic). This algorithm uses trial division optimized to check only up to √n, as any composite number must have a prime factor ≤ √n. Returns a map of prime factors to their powers, e.g., 360 = 2³ × 3² × 5¹.