Why Hex? C++

Hello guys, I recently put an eye on some game projects/ server and client..

but when sending data?! why not sending normal txt? i mean some commands or other stuff, but I always keep seeing this kind of data..

1
2
3
4
5
6
7
8
9
10
11
12
  0xFF ? or 0x20 ? 

or this stuff

#define RPSS_NUMOFUSERS 0x03
#define RPSS_STARTGAME 0x04
#define RPSS_SCISSOR 0x05
#define RPSS_ROCK 0x06
#define RPSS_PAPER 0x07
#define RPSS_QUIT 0x08

of 0xFFFFF wich might be an address of an array?! or memory, but why putting an constant mem-address when everytime u run the program this could be pointing somewhere else?


Pleas I would appriciate a clear explaination.
thanks in advance.
Last edited on
Hexadecimal is just another way to write down numbers. It is often more convenient than decimal, e.g. when we are dealing with numbers that represent byte values or memory addresses. But you really could write down the numbers in either hexadecimal, decimal or octal – whichever you prefer. It's simply a different notation!

Now, when it comes to sending some "commands" over a network, it is much more efficient to encode these commands as numbers rather than text strings. For example, a number like 0x03 requires just a single byte to transmit, where the text string "RPSS_NUMOFUSERS" would require 16 bytes! (assuming ASCII or UTF-8).

Also, for the receiver of the command it is much easier to deal with a numeric value, which, for example, can simply be thrown into a switch() statement, rather than having to parse a text string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while(true)
{
    const uint8_t command = receive_next_command();
    switch(command)
    {
    case RPSS_NUMOFUSERS:
        handle_numofusers_command();
        break;
    case RPSS_STARTGAME:
        handle_startgame_command();
        break;

        /* ... */

    default:
        abort(); /*unknown command*/
    }
}
Last edited on
Hex is often used in addresses and memory dumps. It has a couple of advantages.
1) Since it is a multiple of binary, it's easier to see the bit representation compared to decimal.
2) It is slightly more compact than decimal. A single hex character represents 16 values while a decimal character can only represent 10 values.
hex is often used without reason. Some programmers just like it and use it. Here, someone is wasting energy typing 4 chars for 1. 3,4,5,6,7,8 would have been just as effective without the extra letters.

its shorter than decimal for very large values ... 8+2 characters can represent 19 characters in base 10 for 64 bit ints, half or less of the space in a text file depending on if you kept the 0x or no. The difference in size for encrypt keys or hash (sha 256 etc) is astonishing.

and, it has patterns for both binary and bytes so with experience you can see what is in memory vs what you expected and know if something is correct.

I use it when I need it. I prefer decimal if there is no good reason to use hex.
wow thank you guys 100 times
Topic archived. No new replies allowed.