AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [TUT] Hexadecimal system (https://forums.alliedmods.net/showthread.php?t=164903)

diablix 08-16-2011 21:06

[TUT] Hexadecimal system
 
Quote:

Originally Posted by Wikipedia
Hexadecimal system - In mathematics and computer science, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16.

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 :P

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:

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?

PHP Code:

#include <amxmodx>

#define VERSION "0.1"

new const g_iHEX[] = {
        
0x14,   //20
        
0x25,   //37
        
0x15,   //21
        
0x05,   //5
        
0x50,   //80
        
0xA,    //16
        
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(sMessagesizeof sMessage 1"DEC READEN FROM HEX : ");
        
        for(new 
sizeof g_iHEX ++)
                
iMesSize += formatex(sMessage[iMesSize], (sizeof sMessage 1) - iMesSize"%d "g_iHEX[i]);
        
        
set_pev(idpev_effectsg_iHEX[5]); // 0xA = 16 * A(10) = 16

        
client_print(id3sMessage);
        
        return 
1;


I could make some mistakes cause of my weird english. Sorry bros :crab:

Bugsy 08-16-2011 21:40

Re: [TUT] Hexadecimal system
 
PHP Code:

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

0xA = 10, not 16

Your tut only covers hex values <= 255, which it is typically easier to just use decimals. You should also discuss how larger values are stored which I think will better explain hexadecimal.

You should post a link to a calculator so people can calculate they own values. Also, this link helps. Here's another.

fysiks 08-16-2011 22:08

Re: [TUT] Hexadecimal system
 
Quote:

Originally Posted by diablix (Post 1534229)
It's easier to convert those integers to binary system which is used by computers.

It's easier for who? Why would I need to convert to binary? I've rarely needed to know anything in binary. That's why I use a high-level programming language.

Quote:

Originally Posted by diablix (Post 1534229)
It also looks kinda cooler

Honestly, IMO, it just makes it harder to read.

jim_yang 08-16-2011 22:10

Re: [TUT] Hexadecimal system
 
new sMessage[0x80]
this kind of stuff just makes ur code unreadable, code is for people to read, I don't see any cool thing to declare an array with hex as its size.

Bugsy 08-16-2011 22:56

Re: [TUT] Hexadecimal system
 
I agree with you both, 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

nikhilgupta345 08-16-2011 23:35

Re: [TUT] Hexadecimal system
 
Quote:

Originally Posted by Bugsy (Post 1534270)
I agree with you both, 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

Could you explain 'bit masks' a little bit more and the usage in AMXX scripting?

Thanks.

Exolent[jNr] 08-17-2011 00:19

Re: [TUT] Hexadecimal system
 
Quote:

Originally Posted by nikhilgupta345 (Post 1534288)
Could you explain 'bit masks' a little bit more and the usage in AMXX scripting?

Thanks.

Look into binary.
Bugsy also wrote a tutorial on bits.

fysiks 08-17-2011 00:19

Re: [TUT] Hexadecimal system
 
Thanks Bugsy, that's a really good point.

abdul-rehman 08-17-2011 01:32

Re: [TUT] Hexadecimal system
 
You didnt explained about numbers higher than 0xFF like 0xF0FFF
So can you please cover this part as well ? other wise it will confuse people like me.

jim_yang 08-17-2011 01:46

Re: [TUT] Hexadecimal system
 
what should it cover ? It's just a numeral system, nothing to explain on higher than 0x10


All times are GMT -4. The time now is 13:53.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.