[TUT] Hexadecimal system

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1267
Gender:
United States of America

[TUT] Hexadecimal system

Post by Archive-Poster »

I'll just tell you an usage of this system in PAWN.

I prefer this system instead of decimal system. Why?
It's easier to convert those integers to binary system which is used by computers. It also looks kinda cooler

Some examples of integers in HEX system
0xFF
0x10
0x25
0xA
0xAF
How to convert HEX integer into decimal one?
It is quite easy.

0x10

Just don't mind about this first zero. Imagine that x = 16.

0x10 = x(16) * 1 + 0 = 16

Just multiple first number after x (As I said imagine that x = 16)
The rest integers you have to sum.

We multiplied 16 by 1 and we add there a 0

Some more examples

0x21 = x(16) * 2 + 1 = 33

0xFF = x(16) * 15 + 15 = 255

Oh wait! What is this 'F' character?

In this system you can also use letters instead of numbers.

Code: Select all

A = 10
B = 11
C = 12 
D = 13 
E = 14
F = 15
And now.. why I said don't mind about this 0 ?
Compiler detects that this integer is in HEX System by 0x at the begining.
You don't need to use this zero in conversion.

How would it look in a PAWN script?

Code: Select all

#include <amxmodx>

#define VERSION "0.1"

new const g_iHEX[] = {
        0x14,   //20
        0x25,   //37
        0x15,   //21
        0x05,   //5
        0x50,   //80
        0xA,    //10
        0xFF    //255
};

public plugin_init() {
        register_plugin("HEX TO DEC", VERSION, "diablix")
        register_clcmd("say /test", "cmdHexToDec");
}

public cmdHexToDec(id){
        new sMessage[0x80]; // 0x80 = 16 * 8 + 0 = 128
        new iMesSize = formatex(sMessage, sizeof sMessage - 1, "DEC READEN FROM HEX : ");
        
        for(new i = 0 ; i < sizeof g_iHEX ; i ++)
                iMesSize += formatex(sMessage[iMesSize], (sizeof sMessage - 1) - iMesSize, "%d ", g_iHEX[i]);
        
        set_pev(id, pev_effects, g_iHEX[5]); // 0xA = 16 * A(10) = 16

        client_print(id, 3, sMessage);
        
        return 1;
} 
User avatar
Archive-Poster
Posts: 1267
Gender:
United States of America

Re: [TUT] Hexadecimal system

Post by Archive-Poster »

I would never use hex to replace simple numeric values just for the sake of using hex. Why use 0x13 when 19 will do? It would be especially confusing for those viewing code who are not versed in hexadecimal.

The only time I would agree with his statement is when working with bit masks. In this instance they are actually easier to use than decimal numbers.

1111 1111 = binary
255 = decimal
FF = hex

1111 0000 1111 0000 = binary
61680 = decimal
F0F0 = hex

1111 0000 1111 1111 1111 = binary
987135 = decimal
F0FFF = hex
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests