This is known as positional notation . It's the way we represent numbers.
Decimal
The decimal representation everyone knows, goes from 0 to 9 and how far I know it's because of the number of fingers the human has. It's the way humans grow accustomed. Each position on the left adds the total units of the representational system, ie in the decimal each extra house on the left is worth the number represented 10 times.
Hexadecimal
The hexadecimal representation goes from 0 to 15. This is closer to how the functional computer is. Since everything in the computer is binary, everything goes up with base 2, numbers that programmers consider "round" are 2, 4, 8, 16, 32, 64, 128, 256 and more that this would need more than one byte to represent. It is easier and more linear to represent these numbers based on 16, go from 0 to F (A = 10, B = 11, C = 12, D = 13, E = 14, F = 15). Then 1A
is the same as 26 in decimal since 1 in this case is it times 16 (the total number that can be represented in hexa plus 10 which is the value of A.
Note that to differentiate from the hexadecimal literal in most languages, you should start with 0x
.
Colors are an example to anyone developing to web using hexadecimal. The% color of% is absolute white. The first FFFFFF
says that it is the maximum red, or 255. Then the FF
indicates the maximum green and the last FF
indicates the maximum blue. When all the colors in their fullness are combined, we have the white. To represent the same thing in decimal form would have to be FF
. The comma is required because it is common that the decimal can have a variable number of digits and in hexadecimal it is common to use a fixed number of digits in case 2, since with 2 digits we can represent all possible numbers in a byte .
Octal
Octal was used on computers with a specific architecture, today it has little relevance. It can still be used to represent situations where the base must be 8 (from 0 to 7), but it is rare to have effective use. Therefore to represent decimal 8 in octal would be 10. Note that it follows the hexadecimal model using a binary-based ratio. Many languages no longer support this representation. Normally a leading zero is used to indicate that the representation is octal. This can generate bugs in some cases and the programmer does not take care.
Binary
In addition, some languages use binary representation that is the most basic for computers. This is simple since it has only two possible digits, 0 and 1. Since the base is 2, each extra digit on the left is raised to 2. With 8 digits we have a byte , the same 256 different values when we use 2 digits in hexadecimal. This is a useful representation in limited cases but can be useful when the data is composed of Boolean information in some way, when it needs to show on and off states.
Conversion
In general, to convert from a smaller base to a larger base, the multiplication of each right-to-left digit of the number you want to convert by the number resulting from the base's power by a sequence of numbers starting at 0 (remembering that every number raised to zero results in 1).
To do the opposite operation you will make successive divisions using the base as a divisor and taking the rest of each division. The first part goes to the right and the rest goes to the left.
This is a simplification of conversion. You can not use it in any situation.
Closer to computer pattern
For computers 10 is a strange number, it is a representation used to make the life of a strange animal easier. But at the same time it can create some difficulties.
Almost all languages, from assembly to JavaScript, have at least the decimal and hexadecimal and they should be used when they are most convenient in each case. In PHP it is rarer to use hexadecimal in most cases.
Note that all these numbers represent the same number differently. For computers the number is represented in the same way, ultimately through electrons circling. The representation in the language code is something that only matters for humans to see better what they are doing, it is more an abstraction.