![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I’m looking for an Oracle function to convert a dot-format IP address into its numeric equivalent ('127.0.0.1' --> 2130706433). I’ve googled for "ip2long", "inet_ntoa", "IP to decimal"... but I only get links to the PHP, MySQL or C implementations. I don’t mind writing my own function but my PL/SQL skills are minimum and I can’t go past the initial step of splitting the string into four numbers (I can't find an Oracle equivalent of the split/explode functions available in other languages). How can I proceed? I’m running Oracle 10g XE on Windows XP. Thank you in advance. |
#3
| |||
| |||
|
|
alvaro.NOSPAMTH... (AT) demogracia (DOT) com> wrote: I’m looking for an Oracle function to convert a dot-format IP address into its numeric equivalent ('127.0.0.1' --> 2130706433). ... I’m running Oracle 10g XE on Windows XP. Thank you in advance. snip SELECT TO_NUMBER(REGEXP_SUBSTR('127.0.0.1','\w+',1,1))*PO WER(2,24) + TO_NUMBER(REGEXP_SUBSTR('127.0.0.1','\w+',1,2))*PO WER(2,16) + TO_NUMBER(REGEXP_SUBSTR('127.0.0.1','\w+',1,3))*PO WER(2,8) + TO_NUMBER(REGEXP_SUBSTR('127.0.0.1','\w+',1,4))*PO WER(2,0) IP FROM DUAL; |
#4
| |||
| |||
|
|
Charles Hooper wrote: alvaro.NOSPAMTH... (AT) demogracia (DOT) com> wrote: I’m looking for an Oracle function to convert a dot-format IP address into its numeric equivalent ('127.0.0.1' --> 2130706433). ... I’m running Oracle 10g XE on Windows XP. Thank you in advance. snip SELECT TO_NUMBER(REGEXP_SUBSTR('127.0.0.1','\w+',1,1))*PO WER(2,24) + TO_NUMBER(REGEXP_SUBSTR('127.0.0.1','\w+',1,2))*PO WER(2,16) + TO_NUMBER(REGEXP_SUBSTR('127.0.0.1','\w+',1,3))*PO WER(2,8) + TO_NUMBER(REGEXP_SUBSTR('127.0.0.1','\w+',1,4))*PO WER(2,0) IP FROM DUAL; I'd use \d rather than \w as it is more robust with regards to 'invalid' input... with addresses as ( select '127.0.0.1' ip from dual union all select 'localhost' ip from dual ) select ip, to_number(regexp_substr(ip, '\d+', 1, 1)) * 16777216 + to_number(regexp_substr(ip, '\d+', 1, 2)) * 65536 + to_number(regexp_substr(ip, '\d+', 1, 3)) * 256 + to_number(regexp_substr(ip, '\d+', 1, 4)) from addresses; Note that both are obviously dot-decimal dependant. Older IP systems allowed C style octal (leading 0) or hex (leading 0x). |
![]() |
| Thread Tools | |
| Display Modes | |
| |