This is known as positional notation. It’s the way we represent numbers.
Decimal
To decimal representation Everybody knows it, it goes from 0 to 9 and as far as I know it’s like this because of the number of fingers the human has. It’s the way humans grow used to it. Each position on the left adds the total units of the representational system, ie in the decimal each extra box on the left is worth the number represented times 10.
Hexadecimal
To hexadecimal representation 0 to 15. This is closer to how the computer works. Since everything in the computer is binary, everything is scaling based on 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 becomes easier and more linear to represent these numbers based on 16, to go from 0 to F (A = 10, B = 11, C = 12, D = 13, E = 14, F = 15). So 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 the hexadecimal literal from the decimal, in most languages, it must start with 0x
.
Colors are an example for those who develop for web hexadecimal usage. The color FFFFFF
is the absolute white. The first FF
says it’s the maximum red, or 255. Then the FF
next indicates the maximum green and the last FF
indicates the maximum blue. When all colors in all their fullness are combined, we have the white. To represent the same thing decimally would have to be 255, 255, 255
. The comma is necessary because it is common for the decimal to 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 the possible numbers in a byte.
Octal
Octal was used in computers with a specific architecture, today has little relevance. It can still be used to represent situations where the base should be 8 (from 0 to 7), but it is rare to have effective use. Therefore to represent the decimal 8 in octal would be 10. Note that it follows the hexadecimal model using a ratio based on binary. Some languages no longer support this representation. Usually an initial 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 which 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 interesting when the data is composed of Boolean information in some way, when it needs to show on and off states.
Conversion
In a general way to convert from a smaller base to a larger one, you multiply each digit from right to left of the number you want to convert by the resulting number of the base power by a sequence of numbers starting at 0 (recalling that any number to 0 results in 1).
To do the opposite operation go making successive divisions using the base as divisor and catching the rest of each division. The first rest goes to the right and the rest are placed to your left.
This is a simplification of conversion. Can’t use in any situation.
Closest to the computer pattern
For computers, 10 is a strange number, it is a representation used to facilitate the life of a strange bug, at the same time it can create some difficulties.
Almost all languages, from Assembly Javascript, have at least decimal and hexadecimal and they should be used when they are most convenient in each case. In PHP it is more rare 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 circulating electrons. The representation in the language code is something that matters only for humans to see better what they’re doing, it’s more an abstraction.
Textual representation
What you see of number on the screen is a textual representation of the number and not the number itself. Just like on paper, you have a textual representation, not a number. I know it can be hard to understand this because you’ve always gotten used to understanding that what you’re seeing there on paper is the number, but the number exists in nature, and there are diverse representations of it, you see a pair of stones can represent the number 2. Having electrons in a certain organization can represent numbers, what you see is just a text representing a number.
So when someone talks to convert a number from one notation to another it is usually talking about converting one textual representation to another.
In fact often it’s not even done, it’s just taking a number and generating a certain textual representation. One of the reasons this happens is that it is normal to always have in the language a way to print the text of numbers in a natural way, so that people do not even realize that there is a conversion there. It is common to print in other notations, and would not need to do any algorithm, but teachers and authors often put this as an exercise to do manually. They help amplify the myth that numbers are decimal because they don’t tell you to convert to decimal notation (that’s what I always say, people repeat what they’ve seen others do, they don’t understand what you’re doing).
I think it would look good an example of conversion between them and perhaps an aside with the binary system as well ;)
– Jorge B.
@Jorgeb. soon you come to me to talk about binary, would be off-topic :P You know I thought about putting and not put because I remembered you yesterday :) But I will improve yes.
– Maniero
Ahahah Yes but it is one thing to dwell on the subject, another is to complement the answer. ;)
– Jorge B.