If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Help with math in function

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-23-03, 13:55
DBW-Monica DBW-Monica is offline
Registered User
 
Join Date: Oct 2003
Posts: 2
Help with math in function

I have a function that reads an unsigned integer and translates it into an IP address. The problem is that it is rounding the numbers so the IP numbers are off by 1 either up or down, i.e., 144.xxx.xxx.xx will show as 145.xxx..... or 144.456.xx... will show as 144.455.xx.....Can someone help?

Here is the code:

create or replace function readip(unsint number)
return varchar
is
ip_address varchar(15);
a number(3);
b number(3);
c number(3);
d number(3);

begin
a := mod( ( unsint / 16777216 ), 256);
b := mod( ( unsint / 65536 ), 256);
c := mod( ( unsint / 256 ), 256);
d := mod( ( unsint ), 256);

ip_address := a||'.'||b||'.'||c||'.'||d;

return ip_address;
end readip;

Thank you much!

Monica
Reply With Quote
  #2 (permalink)  
Old 10-23-03, 15:25
dbmadcap dbmadcap is offline
Registered User
 
Join Date: May 2003
Posts: 87
If you want the decimals to be displayed, define the variables as
a number(10,3);

Hope this helps.
Reply With Quote
  #3 (permalink)  
Old 10-24-03, 02:59
cvandemaele cvandemaele is offline
Registered User
 
Join Date: Oct 2003
Location: Switzerland
Posts: 140
Re: Help with math in function

Quote:
Originally posted by DBW-Monica
I have a function that reads an unsigned integer and translates it into an IP address. The problem is that it is rounding the numbers so the IP numbers are off by 1 either up or down, i.e., 144.xxx.xxx.xx will show as 145.xxx..... or 144.456.xx... will show as 144.455.xx.....Can someone help?

Here is the code:

create or replace function readip(unsint number)
return varchar
is
ip_address varchar(15);
a number(3);
b number(3);
c number(3);
d number(3);

begin
a := mod( ( unsint / 16777216 ), 256);
b := mod( ( unsint / 65536 ), 256);
c := mod( ( unsint / 256 ), 256);
d := mod( ( unsint ), 256);

ip_address := a||'.'||b||'.'||c||'.'||d;

return ip_address;
end readip;

Thank you much!

Monica
Ì must be missing the obvious, but I just don't get it. Can you give an example of an input value, and what your function should return ?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On