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.

DecimalHexadecimal
11
22
33
44
55
66
77
88
99
10A
11B
12C
13D
14E
15F
1610
1711
1812
1913
2014
2115
2216
2317
2418
2519
261A
271B
281C
291D
301E
311F
3220
3321
3422
3523
3624
3725
3826
3927
4028
4129
422A
432B
442C
452D
462E
472F
4830
4931
5032
5133
5234
5335
5436
5537
5638
5739
583A
593B
603C
613D
623E
633F
6440
6541
6642
6743
6844
6945
7046
7147
7248
7349
744A
754B
764C
774D
784E
794F
8050
8151
8252
8353
8454
8555
8656
8757
8858
8959
905A
915B
925C
935D
945E
955F
9660
9761
9862
9963
10064

Happy coding!

Follow us:

Leave a Reply

Your email address will not be published. Required fields are marked *