Re: Encoding string for use in sorting and query
Date: Mon, 23 Jul 2001 07:36:10 +0200
Message-ID: <3B5BB7CA.D2F1C25A_at_racon-linz.at>
Joe wrote:
>
> I am trying to create a database in mySQL to track usage of IP addresses.
> However, not being an ace database type, I am stumped as to how to
> encode a string such that I can sort addresses and query accurately
> for a range. If I have the IP address in dotted decimal notation
> represented as a string, does anybody know of a way to easily encode
> that data for storage in another field? The kicker is I also need to
> be able to decode that string back into a dotted decimal notation IP
> address. PHP has a simple function called ip2long() and the opposing
> long2ip() that do the trick, but I don't necessarily want to do all
> the encoding/decoding within a php script. Any help would be
> fantastic.
IMHO, you can use a string without any problems as long as you fill every part
of the address to three characters:
192.168.001.001
You can sort and query those stringes without any problem.
The way you would encode an ip address in a long(?) is that every part of the ip address corresponds to one byte of the number. Then you can also sort and query for ranges (partly!).
hth,
Heinz
Received on Mon Jul 23 2001 - 07:36:10 CEST