Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Hexdecimal format


    Articron

    Recommended Posts

    Just a quicky on the basics on how to form and/or read a hexadecimal format. I'll also include code implementations so the people that learn by reading code will also have an easy time understanding the concept.
     
    Take a look at this little table:
     
    DEC              HEX
    0                      0
    1                      1
    2                      2
    3                      3
    4                      4
    5                      5
    6                      6
    7                      7
    8                      8
    9                      9
    A                     10
    B                     11
    C                     12
    D                     13
    E                     14
    F                     15 
     
     
    1. Converting decimal to hexadecimal formats
     
    Assume we want to convert 250 into a hexadecimal format.
     
    Step 1: We need to grab the 16-modulus of 250
    Note: If you are unfamiliar with modulus, a quick google will get you up to speed with that quite quick.
     
    250 mod 16 = 10 
    (because the highest 16-multiplied value below 250 is 240... 250 - 240 = 10)
     
    Step 2: Use that modulus to know your hexadecimal value from the table
     
    For the value 10, our hex character is 'A' according to the table.
     
    Step 3: Divide our total value(=250) by 16
     
    250 / 16 = 15,625 = 15

    Note: Always round down
     
    Step 4: repeat step 1 - step 3 untill our value of 250 is zero
     
    If we repeat step 1 to 3 for the value 15, our endpoint would be the character 'F'.
     
    Important: we append new characters to our hexadecimal format on the left.
     
     
    Conclusion: 250 = FA
     
    If you were to implement this in code:

     final char[] HEX_TABLE = {
       '0','1','2','3',
       '4','5','6','7',
       '8','9','a','b',
       'c','d','e','f'
     };
    
    String toHexadecimal(int num) {
            int val = num;
            String result = "";
            while (val > 0) {
                int tableId = val % 16;
                result = HEX_TABLE[tableId] + result;
                val /= 16;
            }
            return result;
    }
    

    2. Converting hexadecimal to decimal formats

     

    Assume the following hex format: #1DC

     

    To convert these characters back to the format we like, we'll have to do the opposite of what we did in the previous chapter.

     

    We'll have to multiply the decimal value with 16 to the power of X

    X is determined by the position of our character.

     

    In this case, 1DC has 3 characters.

    1 will be multiplied by 16^2,

    D will be multiplied by 16^1

    C will keep its own value, as it will be multplied with 16^0, which equates to 1

     

    Convert these characters back by the use of the decimal/hex table I gave you:

    1 = 1

    D = 13

    C = 12

     

    Our equation becomes the following:

     

    (1 x 16^2) + (13 x 16^1) + (12 x 16^0) 

    256 + 208 + 12 = 476

     

    Conclusion: 1DC = 476

     

     

    In a code implementation, it would look like this:

    final char[] HEX_TABLE = {
     '0','1','2','3',
     '4','5','6','7',
     '8','9','a','b',
     'c','d','e','f'
    };
    
    int hexToDec(String string) {
            int result = 0;
            char[] hex = string.toCharArray();
            int hexPow = hex.length - 1;
            for (int i = 0; i < hex.length; i++) {
                for (int x = 0; x < HEX_TABLE.length; x++) {
                    if (hex[i] == HEX_TABLE[x]) {
                        result += x * Math.pow(16,hexPow--);
                    }
                }
            }
            return result;
     }
    
    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.