Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Loop through an IP address in Pascal

tekiotekio Posts: 3Member
Is there any function that will convert an IP address to LongINT and vice versa? I know both C and PHP have a standard function for this but canot find any in Pascal.

I've found this great library that claims to do this, inetaux.pas. Though it does a good job reordering the byte order the implementation of the functions StrToAddr() and AddrToString() seem to be flawed, or at least my implementation of them:

sipLng := StrToAddr(sIp);
eipLng := StrToAddr(eIp);


j += 1;
sipLng += 1;
writeln(sipLng,' ',eipLng,' ',j);
until sipLng = eipLng;

Here is a link to the library:

All the writeln() calls are just for debugging. I can't really look at the library and understand it too well. As I'm a newb to Pascal.

I mostly use Free Pascal on Mac and Unix, and Delphi for Windows. I've looked through the documentation of Free Pascal and couldn't find anything.

EDIT: it looks like the function is flawed. Since they each are turned into a signed LongInt sipLng is actually a greater value than the ending IP address, in this case eipLng().
Here is an example with sipLng at and eipLng set to

if I understand this correctly the loop is infinite because the starting ip address was converted to a higher value than the ending ip address.
How do most other developers solve this problem with FreePascal? I'm sure it comes up a lot.


  • Phat NatPhat Nat Posts: 757Member
    This is because it is using a Signed 32-bit number as opposed to an unsigned 32-bit number, which unfortunately Pascal does not directly support (although Delphi does?)

    Also, this method of comparison is not good with the way they have decided to store the IP address as the last number in the IP address is the most important. It's like comparing a birthdates by the day first, then the month, then the year. So if you were born on the 12th and I was born on the 5th, you would be older no matter what.

    It would be beneficial to swap the way the number is stored as a LongInt so that the first number is the most important. However, if the first number of the IP address is larger than 127, it will still turn the number negative.

    In this case you would still have to check :
    If IPADDR1 < 0 Then
    ELSE If IPADDR1 >= 0 Then
    Something like this anyways.

    Looking at the unit again, there is a function to swap the way it is stored:
    WriteLn(IPADDR); { Normal }
    WriteLn(HTON(IPADDR)); { Reversed Storing }
Sign In or Register to comment.