Added explanation on hex to decimal conversion (#21747)
Added an example conversion from hex to decimal, along with an analogy to decimal system.
This commit is contained in:
@ -14,7 +14,30 @@ and use the first six letters of the alphabet to represent the values 10 through
|
|||||||
|
|
||||||
In programming, we prefix hexadecimal constants with `0x`, with some exceptions.
|
In programming, we prefix hexadecimal constants with `0x`, with some exceptions.
|
||||||
|
|
||||||
|
|
||||||
### Examples and explanation
|
### Examples and explanation
|
||||||
|
|
||||||
|
In the standard base 10 system, each column represents increasing powers of 10,
|
||||||
|
while in base 16 each column represents increasing powers of 16.
|
||||||
|
|
||||||
|
Consider the following number in base 10: `1337`
|
||||||
|
|
||||||
|
| 1000 | 100 | 10 | 1 |
|
||||||
|
| :---------- | :---------- | :---------- | :---------- |
|
||||||
|
| 1 | 3 | 3 | 7 |
|
||||||
|
|
||||||
|
It is `one thousand three hundred and thirty seven` because `1*1000 + 3*100 + 3*10 + 7*1`.
|
||||||
|
|
||||||
|
Similarly, consider a hex number (base 16): `0xBEEF`
|
||||||
|
|
||||||
|
| 16^3 | 16^2 | 16^1 | 16^0 |
|
||||||
|
| :---------- | :---------- | :---------- | :---------- |
|
||||||
|
| B (11) | E (14) | E (14) | F (15) |
|
||||||
|
|
||||||
|
Converting to decimal, it would be `11*16^3 + 14*16^2 + 14*16 + 15*1` which gives `48,879`.
|
||||||
|
|
||||||
|
Here are some other examples of equivalent hex and decimal values:
|
||||||
|
|
||||||
```
|
```
|
||||||
0x1 == 1
|
0x1 == 1
|
||||||
0xF == 15
|
0xF == 15
|
||||||
@ -23,9 +46,6 @@ In programming, we prefix hexadecimal constants with `0x`, with some exceptions.
|
|||||||
0x1000 == 4096
|
0x1000 == 4096
|
||||||
```
|
```
|
||||||
|
|
||||||
In the standard base 10 system, each column represents increasing powers of 10,
|
|
||||||
while in base 16 each column represents increasing powers of 16.
|
|
||||||
|
|
||||||
As seen in the table example above, with one hex digit we can represent numbers up to and including 15. Add another column and we can represent numbers up to 255, 4095 with another column, and so on.
|
As seen in the table example above, with one hex digit we can represent numbers up to and including 15. Add another column and we can represent numbers up to 255, 4095 with another column, and so on.
|
||||||
|
|
||||||
## Uses of Hexadecimal in Low Level Programming
|
## Uses of Hexadecimal in Low Level Programming
|
||||||
|
Reference in New Issue
Block a user