Let's talk about numberical systems including hexadecimal and going from decimal to binary, etc.
The numeral system we typically use in our day to day lives is base-ten or decimal. Why do we use base-ten? Probably because we have 10 fingers.
ten millions | millions | hundred thousands | ten thousands | thousands | hundreds | tens | ones | |
---|---|---|---|---|---|---|---|---|
decimal | 107 | 106 | 105 | 104 | 103 | 102 | 101 | 100 |
128s | sixty fours | thirty twos | sixteens | eights | fours | twos | ones | |
---|---|---|---|---|---|---|---|---|
binary | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 |
65,536s | 4,096s | 256s | 16s | ones | ||||
---|---|---|---|---|---|---|---|---|
hexadecimal | 167 | 166 | 165 | 164 | 163 | 162 | 161 | 160 |
|||||262144's|4096's|64's|ones| |------------|------------|--------|-----------------|-------------|---------|--------|----|----| |base64|647|646|645|644|643|642|641|640|
In the table, you can see we have ones, tens, hundreds, thousands, etc. In each spot for the decimal system, we can put 10 digits, which are 0 through 9. So, for example when we have 9 ones and add 1, we then have 1 ten and 0 ones, represented as 10. If I said I had twelve dollars, that's a 1 in the tens spot and a 2 in the twos spot. Reviewing these principles will help in understanding how binary and hexadecimal work.
So, how does the decimal (or base-ten) system work? We can put 1 digit in each spot, and it can be one of 10 values from 0 through 9. Likewise, the binary (or base-two) system has 1 digit in each spot, which can be one of 2 values, from 0 through 1, and the hexadecimal (or base-sixteen) system can also has 1 digit in each spot, which can be one of 16 values.
Since 10 through 15 are actually 2 characters, we use a through e instead, so the sixteen values are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e.
So, we can use binary and hexidecimal to represent numbers, just like we can with decimal, though we have different amounts of values we can store in each digit.
Let's look at what some numbers look like in the various systems
decimal | binary | hexadecimal |
---|---|---|
0 | 0 | 0 |
1 | 1 | 1 |
5 | 101 | 5 |
10 | 1010 | a |
15 | 1111 | e |
50 | 110010 | 32 |
100 | 1100100 | 64 |
decimal to hexadecimal and binary conversion calculator
Here's a binary joke, "There are 10 types of people in the world; those who understand binary and those who don't."
To represent 42,420 we need 4 ten thousands, 2 thousands, 4 hundreds, and 2 tens:
ten millions | millions | hundred thousands | ten thousands | thousands | hundreds | tens | ones |
---|---|---|---|---|---|---|---|
107 | 106 | 105 | 104 | 103 | 102 | 101 | 100 |
4 | 2 | 4 | 2 | 0 |
How would we represent 42 in binary?
128's | sixty fours | thirty twos | sixteens | eights | fours | twos | ones |
---|---|---|---|---|---|---|---|
27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 |
1 | 0 | 1 | 0 | 1 | 0 |
And how about representing the number 911 in hexadecimal
65,536s | 4,096s | 256s | 16s | ones |
---|---|---|---|---|
164 | 163 | 162 | 161 | 160 |
3 | 8 | F |
To get 911 in base-sixteen, we have 3 times 256 which equals 768 plus 8 times 16 which equals 128 and 15 (represented by F) times 1 which equals 15. 768 + 128 + 15 = 911.
So now that we know how decimal, binary, and hexadecimal work, let's see it in action in Go:
package main
import (
"fmt"
)
func main() {
s := "H"
fmt.Println(s)
/* convert s to a slice of bytes */
bs := []byte(s)
fmt.Println(bs)
/* assign n the value of the first slice of bytes in bs */
n := bs[0]
fmt.Println(n)
/* print the type of n */
fmt.Printf("%T\n", n)
/* print n as binary */
fmt.Printf("%b\n", n)
/* print n as hexadecimal */
fmt.Printf("%x\n", n)
/* print n as hexadecimal with indicator prefix */
fmt.Printf("%#X\n", n)
}