Re: [Plugins-writers] converting hex to dec

From: David Kyger ((no)
Date: Sun Mar 14 2004 - 19:45:56 PST

  • Next message: George Theall: "Re: [Plugins-writers] converting hex to dec"

    On Sat, Mar 13, 2004 at 06:17:47PM -0500, dave@private wrote:
    > 
    > I'm pulling values from specific places in a hex string, such as:
    > 
    > mystring = '006f004e6573737573';
    > 
    > myport = mystring[0] + mystring[1] + mystring[2] + mystring[3];
    > 
    > 
    > Is there a function available to convert the value of "myport" to its decimal equivalent?
    > 
    
    I wanted to take a shot at it; this is what I came up with. There's probably a better way to do it though.
    
    function hex2dec(s)
    {
    local_var i, j, x, var, conv, int, sum, ret;
    
    conv =   make_array('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'
                       );
    
    j = strlen(s) - 1;
    x = 0;
    
    for(i=0;i<strlen(s); i+= 1)
    {
            foreach var (keys(conv)) {
                    if (var == s[i]) {
    
                            if (var == 0) {
                                    j -= 1;
                                    x += 1;
                            }
                            else {
                                    if (x != strlen(s) -1) {
                                            int = int(conv[var]);
                                            sum += (int * (16 ** j));
                                            j -= 1;
                                            x += 1;
                                    }
                                    else  {
                                            int = int(conv[var]);
                                            sum += int;
                                            }
                                    }
                            }
                    }
            }
    return sum;
    }
    
    -dave
    _______________________________________________
    Plugins-writers mailing list
    Plugins-writers@private
    http://mail.nessus.org/mailman/listinfo/plugins-writers
    



    This archive was generated by hypermail 2b30 : Sun Mar 14 2004 - 19:07:26 PST