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.
// 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
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)
but its just represented as eight groups of four hexadecimal digits, each group representing 16 bits (two octets). The groups are separated by colons (
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.