Storing / Retrieving An IP Address

Good evening all,

I’ve been looking around the net for the best ways to store an IP address, and got to the following to store the full IP address into a single Int64 value ready to be stuffed into an sqlite database, and then again to convert it back to human readable form.

// Convert values from octets to single Int64 for storage

Dim IPAddress, Octet1, Octet2, Octet3, Octet4 As Int64 = 0

// Code here to validate the individual octets and put those values into Octet1, Octet2, Octet3, Octet4 defined above.

IPAddress = ((Octet1 * 16777216) + (Octet2 * 65536) + (Octet3 * 256) + Octet4)

// Convert Int64 value back to individual octets ready to be displayed.

Dim IPAddress as Int64 = 3232235521 ’ This value would have been retrieved from the database; in this case, a commonly used IP address.
Dim Octet1, Octet2, Octet3, Octet4 As Int64 = 0

Octet1 = IPAddress \ 16777216
Octet2 = (IPAddress - (Octet1 * 16777216)) \ 65536
Octet3 = (IPAddress - ((Octet1 * 16777216) + (Octet2 * 65536))) \ 256
Octet4 = (IPAddress - ((Octet1 * 16777216) + (Octet2 * 65536) + (Octet3 * 256)))

// Code here to display Octet1, Octet2, Octet3, Octet4 on display form

Any thoughts if I am on the right track or is there a more efficient way to do this ? Thanks.

Why not just store it as the full dotted string ?
Its not a “number” in anything that requires it in Xojo anyway so you have to convert it back & forth - or just use the string and do no conversions
And a number wont handle something like “www.domain.com” but a string will

Well I was thinking from the most efficient way for storing in the database. Domain names are not part of the requirement since the IP addresses that are stored will be mainly for printers and other attached peripherals.

Even on an internal LAN they could still use named addresses depending on how your network is set up

I would worry about storing it in the most usable manner

And if you still want it as a number I might do

Dim IPAddress as int32 // IPV4 addresses are 4 octets or 32 bits

// from octets to one 32 bit value
dim mb as memoryblock(4) // since an IP address is 4 octets or 32 bits
mb.littleEndian = true
mb.byte(0) = Octet1
mb.byte(1) = Octet2
mb.byte(2) = Octet3
mb.byte(3) = Octet4
IPAddress = mb.Int32Value(0)

and the other way

dim mb as memoryblock(4) // since an IP address is 4 octets or 32 bits
mb.littleEndian = true
mb.Int32Value(0) = IPAddress
Octet1 = mb.byte(0)
Octet2 = mb.byte(1)
Octent3 = mb.byte(2)
Octet4 = mb.byte(3) 

Thanks Norman, I’ll give that a go.

It should be close if not exactly right
I just wrote it in my reply and haven’t tested it so …

[quote=297598:@Norman Palardy]It should be close if not exactly right
I just wrote it in my reply and haven’t tested it so …[/quote]

Thats cool. I see what you are suggesting with the code. I can tweak a bit if needed.

but that won’t work for ipV6 just ipV4

v6 is just 128 bits instead of 32

but its just represented as eight groups of four hexadecimal digits, each group representing 16 bits (two octets). The groups are separated by colons (:slight_smile:

nbd

You knew that, and I knew that. but the point was, the code AS POSTED, is for ipV4, and if the poster was unaware it would be an issue

I gave code that would simplify what he was already doing for an “IPaddress” which he was using as 4 dotted octets

I assumed he realized this is an IPv4 address only

Thanks for the continued input. Yep, I am aware the code is in respect of IPV4. There is no IPV6 setup on the private network it is intended for. The stored addresses will mostly relate to printers and a small number of servers.