How do hexadecimal numbers work?

Asked

Viewed 38,176 times

66

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

It is not anywhere that one finds a programmer who can read hexadecimal decently and much less make use of the numbers. But hexadecimals are quite common, mainly in systems programming, low-level programming and information security:

  • Exploits payloads need options in hexadecimal
  • Hexdumps, the name says it all.
  • I don’t know why, but hexadecimals are also fairly present in C macros.

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

2 answers

87


Numbers are simply numbers

It may sound strange, but there’s not much to learn in hexadecimal that’s different from the math 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 any other basis, is the same. What changes is the amount of symbols to represent the digits.

" Incidentally", as our common Arabic digits only range from 0 to 9, we have chosen to add the letters A to F to represent the extra digits, since hexadecimal is a 16-digit notation, instead of the 10 we are used to.

Instead of creating a new collection of drawings to represent the numbers after the 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;
  • on the basis of 36 we go further still, from 0 up to the letter Z!
  • in binary, goes only from 0 to 1, but the logic is still the same.

Just like the last decimal number, which is 9, added to 1 turns 10 (i.e., goes back to zero and gains a new "house" next door), if we take the last "number" of Hexa, which is "F" and add "1", the result is 10 in hexadecimal. Similarly, in binary (which consists 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

Summary: mathematics is the same.

In informatics

The Hexadecimal is very present in the computer by the ease that has to represent the numbers of another base: the binary.

Since both hexadecimal and binary are power numbers of two, you can reconcile some concepts of both bases interestingly. For this, let’s start from this tabelinha:

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 on reaching 15 decimal, both hexadecimal and binary arrived at the last house before "turning" another house. As 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 hexadecimal digit. Knowing or understanding how to count these 16 digits, we convert any hexadecimal to binary and vice versa.

So, looking at the above table, 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 the hexadecimal enters this history then?

The binary is the basis of the current technology, because in both Hds and memory, or in processors, the information is represented as "on" or "off" only (the so-called bits).

This doesn’t apply to quantum processors, but wait a little longer to have one of these on your desk.

For some time now, processors and memory in general have used spaces to store information organized in 4-bit packages (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 enough);
  • to represent one byte, only two of them, from 00 to FF;
  • and so we follow, for "measures" such as 0000 to FFFF, and larger.

Examples "Visual":

  • It is much easier to write 5F than 01011111, no?
  • A MAC Address Network card: 00-5F-FF-E0-AA-FF instead of 0-95-255-224-170-255
  • Color #F00CC instead of rgb(255,0,204);
    perhaps in this case the hexadecimal causes initially strangeness, 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 bytes from 0 to 255 instead of 00 to FF

Example of hexdump:

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 houses, or separators to identify the numbers.

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


How to "read" the hexadecimal?

A: Without fear :)

In the same way that 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.

It seems complex at first glance, but after you get used to the values from A to F, the rest becomes habit (in the same way we get used to the example of 0 to 9 in day-to-day life).

Just to complete the reasoning, see another example, this time in binary:

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

Kept to proper proportions, it’s like that Cypher scene in the 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."

  • Very enlightening! I didn’t even know that hexadecimal notation made binary notation easier and more readable! Now it seems so obvious: 10 = 111111110000000. And, I didn’t understand your reference to the scene. I watch Matrix, but I don’t remember what that scene is. Does the speech in you actually refer to women?

  • 2

    @Sid the Cypher is looking at the monitor, with those green symbols, and Neo asks, "Do you always see encoded?" and then Cypher replies (briefly): "The Matrix is too complex to decode in real time, but then you get used to it. I don’t even see the code anymore, I see blonde, brunette, redhead..."

  • 1

    By the way, 240 dec = F0 hex = 11110000 bin and 10 dec = 0A hex = 00001010 bin. 1111111100000000 = FF00, that is to say, 65280 decimally. Separate to read: 1111 1111 0000 0000 = F F 0 0, that is to say (15 * 16 * 16 * 16 ) + ( 15 * 16 * 16 ) + ( 0 * 16 ) + ( 0 ) = 65280

  • Excellent reply, remember almost every case! + 1

  • 8

    I don’t Even see the code. All I see is 0xB00B5

  • 2

    Just fantastic this explanation

  • Perhaps it was worth remembering that on older architectures (such as the PDP-10 where the C was created), the "natural" unit was a group of 36 bits, and so on these architectures it was very common to use base octal 0-7, since a PDP-10 word is made up of six groups of octal two digits - hence the use of octals in Unix permissions and the support for this base in C.

  • @Wtrmute is a good observation. I don’t know if the question of the PDP-10 would have much to do with the original question to go into detail, but somehow, when it comes to tinkering with the answer more calmly, I think it pays at least for examples in octal as well, until pq is a basis that confuses the staff.

  • 1

    Great explanation. I like to think that the decimal is a box yesterday fit 10 items and to add more items would need another box. Hexadecimal is a box that fits 16 items and binary only two.

Show 4 more comments

4

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

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

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

To create the constants each one using a different bit basically you start from the 1 and go doubling the values, very simple, a very easy account to do head with small numbers, however in hexadecimal you do not need to count any, just need to memorize 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 an extra zero every 4 values, the next hexadecimal value would be 0x100, and becomes really useful when you have many flags, for example if you are in the flag 0x4000 don’t need to do any calculation to know that the next one is the 0x8000, if you used decimal you would be in the flag 16384 and it’s no longer so simple to bend her value head, if you don’t pay attention can even miss the value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.