I am storing the IP addresses of systems that access a database. It needs the ability to be searched and and also converted to a human readable format. I was leaning towards have 4 fields IPoct1, IPoct2, IPoct3, IPoct4 all of tinyINT unsigned / Byte type. This way I can search by network and it retains human readability. But I wasn't sure if this was a proper way to store data (4 fields that technically hold data that 1 could).
I could see searching on just the third octet. Around here, we have set up the network addresses to be on the 10.0.0.0 network, which is a 'non-network" according to the powers that be. As such, the third octet came out to be the location for us. My building has 10.0.101.0 and 10.0.102.0 for clients. Easy enough to trace.
I'm off work until Monday so I dont have the exact code, but to sum it up:
IPasINT = octet[X](X=0to3) * 256 ^ X(0to3)
This creates an unsigned integer. It is important to note that this in not the same format that Windows uses, although if read in binary it would be... Windows uses a signed int therefor 22.214.171.124 and greater would be negative numbers as signed integers.
Those NTOA and ATON functions are expecting the signed int I believe. I do know they just convert the order between network order and host order.
*edited for correct math*
The conversion to signed int went something like:
If IPasINT > 4294967296 Then IPasSINT = IPasINT - 4294967296
If IPasINT <= 4294967296 THEN IPasSINT = IPasINT
To revert it to a human readable format from an unsigned int was a little trickier. The correct way is to do modulus division but I did it like this:
Loop X from 3 to 0
octet[X] = IPasINT / (256 ^ X) just ignore decimal part ALWAYS round down
IPasINT = IPasINT - (octet[X] * (256 ^ X))
Last edited by kalmon; 03-31-08 at 13:15.
Reason: Memory is not what it used to be and math was WAY off