Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2003
    Posts
    2

    Unanswered: 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

  2. #2
    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.

  3. #3
    Join Date
    Oct 2003
    Location
    Switzerland
    Posts
    140

    Re: Help with math in function

    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 ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •