View Raw SPL
/* convert hex string to integer */
hex2int(hval, nbytes)
{
        local ival = 0, fmt, hex_bitmode;

        if (argc < 2)
        {
                if (argc < 1) error("hex2int - input hex string required");

                nbytes = -1;
        }

        hval = caststring(hval);

        if (nbytes > 0)
        {
                ival = sscanf(hval, "%llx");

                mask = 2^(8 * nbytes) - 1;

                if (mask <= bitmax)
                {
                        ival &= mask;
                }
        }
        else
        {
                /* check configuration mode */
                hex_bitmode = castint(getconf("hex_bitmode"));
                len         = strlen(hval);
                limit       = (hex_bitmode & 0x01) ? 10 : 9;

                if (hex_bitmode && len > limit)
                {
                        /* 64 bit */
                        fmt = "%llx";
                }
                else
                {
                        fmt = "%lx";
                }

                ival = sscanf(hval, fmt);
        }
        
        return(ival);
}