How do hexadecimal numbers work?

I have seen that software libraries written in C, in general, often use hexadecimal numbers. And Assembly programmers also need to be ninjas with this number system.

It is not anywhere that you find a programmer who can read hexadecimal decently, let alone make use of numbers. But hexadecimals are quite common, especially in system programming, low-level programming and security of information:

  • exploit Payloads need the opcodes in hexadecimal
  • HexDumps, the name already says it all.
  • I don't know why, but hexadecimals are also quite present in C macros.

Considering the importance of this system, I ask: How do hexadecimal numbers work?

Author: bfavaretto, 2014-04-21

2 answers

Numbers are simply numbers

It may seem strange, but there is not much to learn in hexadecimal that is different from the mathematics you already know.

The mathematics of binaries, decimals, hexadecimals (which is the same as "base 16"), base 32, base 36, base 64, as well as of any other base, is the same. What changes is the amount of symbols for to represent the digits.

" By chance", like our Arabic digits from 0 to 9, we chose to add the letters A to F to represent the extra digits, since hexadecimal is a notation with 16 digits, instead of the 10 we are used to.

Instead of creating a new collection of drawings to represent the numbers after 9, we used what was already ready, for convenience-the letters - but giving them a new meaning.

  • in decimal, we use the Arabic digits 0 1 2 3 4 5 6 7 8 9;
  • in hexadecimal, we use the digits 0 1 2 3 4 5 6 7 8 9 A B C D E F;
  • in base 36 we go even further, from 0 to the letter Z!
  • in binary, it only goes from 0 to 1, but the logic is still the same.

Just like the last number of the decimal, which is 9, added to 1 turns 10 (i.e., turns pro zero and gets a new "house" next to it), if we take the last "number" of the Hexa, which is "F" and add "1", the result is 10 in hexadecimal. Similarly, in the binary (which is composed of 2 digits, " 0 " and " 1" respectively), adding 1 + 1 we have 10 in binary.

DECIMAL         HEXADECIMAL       BASE36
9 + 1 = 10      F + 1 = 10        Z + 1 = 10

Abstract: mathematics is the same.

In computing

Hexadecimal is very present in computer science because of the ease of representing numbers from another base: binary.

Since both hexadecimal and binary are power numbers of two, you can reconcile some concepts of both bases in an interesting way. For this, let's start from this table:

Decimal Hexa Binário
      0    0       0
      1    1       1
      2    2      10
      3    3      11
      4    4     100
      5    5     101
      6    6     110
      7    7     111
      8    8    1000
      9    9    1001
     10    A    1010
     11    B    1011
     12    C    1100
     13    D    1101
     14    E    1110 
     15    F    1111 <-- Atenção a este caso!
     16   10   10000
     17   11   10001
     18   12   10010
     19   13   10011
     ...

Note that when arriving at 15 decimal, both Hex and binary arrived at the last house before "turning" one more house. Since this" turn " coincides, hexadecimal has become a very practical way of representing binary numbers.

For each of the 4-digit binaries (0000 to 1111), we have exactly one digit of the hexadecimal. Knowing or understanding how to count these 16 digits, we convert any hexadecimal to binary and vice versa.

Thus, looking at the table above, it becomes easy to convert a binary number:

   111010110110110  -> original em binário
111 0101 1011 0110  -> dividido em grupos de nibbles
  7    5    B    6  -> substituído pelos números da tabela.
              75B6  -> reescrito em hexadecimal


Where does the HEX go into this story then?

The binary is the basis of current technology, since both in the HDs and in memory, or in processors, the information is represented as" on "or" off " only (the notorious bits).

This does not apply to quantum processors, but wait a bit longer to have one of these in your desk.

It has been some time since processors and memory in general have used spaces to store information organized in packages of 4 bits (called nibble ), 8 bits ( byte), 16 bits ( word), 32 bits ( dword), and so on.

  • to represent a nibble (0000 to 1111), just one hexadecimal digit (0 to F) is sufficient;
  • to represent a byte, two of them are sufficient, from 00 to FF;
  • e so we follow ,for the "measures" like 0000 to FFFF, and higher.

"Visual" Examples:

  • it's much easier to write 5F than 01011111, right?
  • a MAC address network card: 00-5F-FF-E0-AA-FF instead of 0-95-255-224-170-255
  • color # FF00CC instead of rgb(255,0,204);
    perhaps in this case the hexadecimal causes awkwardness initially, but as the colors for the 216 web palette are multiples of 51 decimal (which is the same as 33 hexadecimal), we have the most common digits being 00, 33, 66, 99, cc and ff, which are abbreviated to one digit each (#33cc00 is the same as #3C0 for browsers).
  • a hexdump would be very confusing if we represented the bytes from 0 to 255 instead of 00 to FF

Hexdump example:

00000000  255044462D312E34 0A312030206F626A  %PDF-1.4.1 0 obj
00000010  0A3C3C0A2F426173 65466F6E74202F48  .<<./BaseFont /H
00000020  656C766574696361 0A2F456E636F6469  elvetica./Encodi
00000030  6E67202F57696E41 6E7369456E636F64  ng /WinAnsiEncod
...

Imagine if we had to use 3 boxes, or separators to identify the numbers.

Thus, it has been agreed to use hexadecimal for many things in which it facilitates reading and the context of information.


how to "read" the hexadecimal?

R: without fear :)

In the same way as in decimal each "house" is multiplied by powers of ten:

279  =  ( 2 * 10 * 10 ) + ( 7 * 10 ) + ( 9 )

In hexadecimal just multiply by powers of 16:

3AF     =     ( 3 * 16 * 16 ) + (  A * 16 ) + (  F )
é o mesmo que ( 3 * 16 * 16 ) + ( 10 * 16 ) + ( 15 ) em decimal.

Seems complex at first glance, but after you get used to the values from A to F, the rest becomes habit (the same way we get used to the example from 0 to 9 on a day-to-day basis).

Only for complete the reasoning, see another example, this time in binary:

1101 = ( 1 * 2 * 2 * 2 ) + ( 1 * 2 * 2 ) + ( 0 * 2 ) + ( 1 )

Kept in proper proportions, it's like that cypher scene in Matrix: "I don't even see the code. All I see now is blonde, brunette, redhead." 1

1. "I don't even see the code anymore. Now I just see blonde, brunette, redhead."

 87
Author: Bacco, 2020-10-07 02:26:05

A good use of hexadecimal when it comes to programming is for the creation of flags, as for example with the Windows API, when you will show a message box and if you want it to be of the Type Yes/No and that the question icon is shown you will pass as a parameter to the function the flags MB_YESNO | MB_ICONQUESTION.

These constants have the values 0x4 and 0x20 respectively, which in binary would correspond to 100 and 100000, so when makes the OR of this values you combine the bits in a way that is easy to check which were the flags used.

But this can be done with decimal numbers in the same way, after all they are basically the numbers 4 and 32, the advantage of using hexadecimal is even time to create these constants.

To create the constants each using a different bit basically you start from 1 and will double the values, very simple, A Very Easy Account to make of head with small numbers, but in hexadecimal you do not need to make any account, you only need to decorate the sequence of the first four flags , follows a table comparing the values, hexadecimal, decimal and binary:

0x01 =   1 = 00000001
0x02 =   2 = 00000010
0x04 =   4 = 00000100
0x08 =   8 = 00001000
0x10 =  16 = 00010000
0x20 =  32 = 00100000
0x40 =  64 = 01000000
0x80 = 128 = 10000000

And it will always follow this same pattern, adding one more zero every 4 values, the next hexadecimal value would be 0x100, and it becomes really useful when you have many flags, for example if you are in flag 0x4000 you do not need to do calculation some to know that the next one is 0x8000, if you used decimal you would be in the flag 16384 and it is no longer so simple to double the value of it head on, if you do not pay attention you can even miss the value.

 4
Author: Leandro Godoy Rosa, 2018-02-21 15:43:09