Funzione dec2string ($ decimale, $ base) { global $ errore; $ String = null; $ Base = (int) $ base; if ($ base <2 | | $ base> 36 | | $ di base == 10) { echo 'BASE deve essere compreso tra 2-9 e 11-36'; uscita; } Charset = $ '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ '; $ Charset = substr ($ charset, 0, $ base); if (! ereg ('(^ [0-9] {1,16} $)', trim ($ decimale))) { $ Error ['dec_input'] = 'Il valore deve essere un numero intero positivo'; return false; } do { $ Resto = bcmod ($ decimale, $ base); $ Char = substr ($ charset, $ resto, 1); / / ottiene CHAR dall'array $ String = "$ $ char string"; / / prepend all'uscita $ Decimale = bcdiv (bcsub ($ decimale, $ resto), $ base); } While ($> decimale 0); return $ string; } Funzione string2dec ($ string, $ base) { global $ errore; $ Decimale = 0; $ Base = (int) $ base; if ($ base <2 | | $ base> 36 | | $ di base == 10) { echo 'BASE deve essere compreso tra 2-9 e 11-36'; uscita; } Charset = $ '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ '; $ Charset = substr ($ charset, 0, $ base); $ String = trim ($ stringa); if (empty ($ stringa)) { $ Error [] = 'stringa di input è vuota'; return false; } do { $ Char = substr ($ stringa, 0, 1); / / estrae personaggio principale $ Stringa = substr ($ stringa, 1); / / drop protagonista $ Pos = strpos ($ charset, $ char); / / recupero di offset in $ charset if ($ pos === false) { $ Error [] = "Carattere non valido ($ char) nella stringa di input"; return false; } $ Decimale = bcadd (bcmul ($ decimale, $ base), $ pos); } While ($ stringa <> null); return $ decimale; } Funzione pad ($ str, $ num) { tornare str_repeat ("0", $ num - strlen ($ str)) $ str.; } / / Main if (isset ($ _GET ["submit"])) { $ Ip = 0; if (isset ($ _GET ["int"]) && $ _GET ["int"]! = "") { $ Ip = $ _GET ["int"]; } else if (isset ($ _GET ["dot"]) && $ _GET ["dot"]! = "") { $ Ip = ip2long ($ _GET ["dot"]); } altro { $ Ip = 0; } Uns_ip = $ ($ ip> 0 $ ip: 0xffffffff + $ ip + 1) + 0; if (($ uns_ip & 0 × 80000000) == 0) { $ Mask = 0xFF000000; } else if (($ uns_ip & 0xC0000000) == -2147483648) { $ Mask = 0xFFFF0000; } else if (($ uns_ip & 0xE0000000) == -1073741824) { $ Mask = 0xffffff00; } altro { $ Mask = ""; } $ Dot_ip = long2ip ($ uns_ip); $ Hex_ip = dec2string ($ uns_ip, 16); Hex_ip $ = "0x" pad ($ hex_ip, 8).; $ Oct_ip = dec2string ($ uns_ip, 8); Oct_ip $ = "0" pad ($ oct_ip, 16).; $ Bin_ip = dec2string ($ uns_ip, 2); $ Bin_ip = pad ($ bin_ip, 32); ?> Conversione risultato | Dot notazione | =$dot_ip?> |
|---|
| Numero decimale | =$ip?> |
|---|
| Numero decimale senza segno | =$uns_ip?> |
|---|
| Numero esadecimale | =$hex_ip?> |
|---|
| Numero binario | =$bin_ip?> |
|---|
| Numero ottale | =$oct_ip?> |
|---|
| Netmask naturali | echo "$mask (".long2ip($mask).") " ?> |
|---|
} ?> | |