Printing hexadecimal in Go
In this article will focus on how to print hex decimal values in the console. The hexadecimal is on the base 16 numeral system.
The code snippet below illustrate how to print hexadecimal values in Go using the fmt.Printf()
function and also how to only retrieve only the resulting string.
decimalValue := 1
fmt.Printf("Hexadecimal: %x\n",decimalValue) // hexadecimal lower case -> 1a
fmt.Printf("Hexadecimal capital: %X\n",decimalValue) // hexadecimal upper case -> 1A
hexadecimalValue := fmt.Sprintf("%c", decimalValue) // If you only want the resulting string.
fmt.Println("Hexadecimal value: ", hexadecimalValue) -> Hexadecimal value: 1a
The %x
format specifier provide the ability to print base 16 numbers.
The table below illustrates hexadecimal number from 1 to 64 which is equivalent to decimal values from 1 till 100.
Decimal | Hexadecimal |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 8 |
9 | 9 |
10 | A |
11 | B |
12 | C |
13 | D |
14 | E |
15 | F |
16 | 10 |
17 | 11 |
18 | 12 |
19 | 13 |
20 | 14 |
21 | 15 |
22 | 16 |
23 | 17 |
24 | 18 |
25 | 19 |
26 | 1A |
27 | 1B |
28 | 1C |
29 | 1D |
30 | 1E |
31 | 1F |
32 | 20 |
33 | 21 |
34 | 22 |
35 | 23 |
36 | 24 |
37 | 25 |
38 | 26 |
39 | 27 |
40 | 28 |
41 | 29 |
42 | 2A |
43 | 2B |
44 | 2C |
45 | 2D |
46 | 2E |
47 | 2F |
48 | 30 |
49 | 31 |
50 | 32 |
51 | 33 |
52 | 34 |
53 | 35 |
54 | 36 |
55 | 37 |
56 | 38 |
57 | 39 |
58 | 3A |
59 | 3B |
60 | 3C |
61 | 3D |
62 | 3E |
63 | 3F |
64 | 40 |
65 | 41 |
66 | 42 |
67 | 43 |
68 | 44 |
69 | 45 |
70 | 46 |
71 | 47 |
72 | 48 |
73 | 49 |
74 | 4A |
75 | 4B |
76 | 4C |
77 | 4D |
78 | 4E |
79 | 4F |
80 | 50 |
81 | 51 |
82 | 52 |
83 | 53 |
84 | 54 |
85 | 55 |
86 | 56 |
87 | 57 |
88 | 58 |
89 | 59 |
90 | 5A |
91 | 5B |
92 | 5C |
93 | 5D |
94 | 5E |
95 | 5F |
96 | 60 |
97 | 61 |
98 | 62 |
99 | 63 |
100 | 64 |
Happy coding!
Follow us: