create or replace function number2ip(num number)
return varchar2 is
ip_num_hex varchar2(8);
begin
ip_num_hex := lpad(trim(to_char(num, 'XXXXXXXX')), 8, '0');
return to_number(substr(ip_num_hex, 1, 2), 'XX') || '.' ||
to_number(substr(ip_num_hex, 3, 2), 'XX') || '.' ||
to_number(substr(ip_num_hex, 5, 2), 'XX') || '.' ||
to_number(substr(ip_num_hex, 7, 2), 'XX');
exception
when others then
dbms_output.put_line(sqlerrm);
return null;
end;
===========================================
create or replace function ip2number(ip varchar2)
return number
is
ip_num_hex varchar2(80);
begin
if (regexp_like(ip, '^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$')) then
ip_num_hex := lpad(trim(to_char(regexp_replace(ip, '^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$', '\1'), 'XX')),2,'0') ||
lpad(trim(to_char(regexp_replace(ip, '^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$', '\2'), 'XX')),2,'0') ||
lpad(trim(to_char(regexp_replace(ip, '^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$', '\3'), 'XX')),2,'0') ||
lpad(trim(to_char(regexp_replace(ip, '^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$', '\4'), 'XX')),2,'0');
return to_number(ip_num_hex, 'XXXXXXXX');
else
return -1;
end if;
exception
when others then
return -1;
end;
============================================
/**
* 将IP地址转换为数值类型.
* @param ipAddress 格式 192.168.0.1
* @return
*/
public static Long ipAddressToLong(String ipAddress) {
String[] ipAdd = ipAddress.split("[.]");
if (ipAdd.length != 4) {
return Long.valueOf(-1);
}
Long result = 0L;
for (int i = 0; i < ipAdd.length; i++) {
result += Long.parseLong(ipAdd[i])<< 24-i*8;
}
return result;
}