How many characters can an IP address be?

12

I have to create a field in a mysql table to store ip addresses. I need to know the maximum characters an ip address can possibly be. Whether ipv4, ipv6 or some other type that can render a valid ip address unusually long. So what should I set the character limit to?

I am asking for length in the sense that the word "foo" has 3 characters, not in terms of bytes or anything like that.

Lonand

Posted 2012-01-21T23:52:27.563

Reputation: 123

Please move to stackoverflow.com: it is a programming question – usr-local-ΕΨΗΕΛΩΝ – 2014-11-07T14:46:20.170

Here is an answer showing the IPv6 address can be up to 45 characters long: https://stackoverflow.com/questions/1076714/max-length-for-client-ip-address#answer-7477384

– still_dreaming_1 – 2017-07-25T16:41:11.210

Answers

20

An IPv6 address, at its longest, would be 8 sets of 4 characters (32 total), each set separated by a colon. That makes 39 characters.

An IPv4 address is at most 4 sets of 3 numbers (12 characters), each set separated by a dot (.). That makes 15 characters.

All of this assumes the IP addresses should be stored in human-readable form. I prefer to store IP addresses in decimal form, as you never quite know how it will be used. Your mileage may vary.

Patrick Seymour

Posted 2012-01-21T23:52:27.563

Reputation: 7 662

Sorry for the necropost, but actually an IPv6 can be up to 45 characters long in some cases of IPv4-mapped IPv6 addresses, since it is prefixed with :<ipv4 original address> – axelduch – 2019-08-15T22:58:06.067

Thanks a lot, human readable form is fine for my use case as I am never going to use them generally. I am only storing the ips of people who signup on my site in case they ever upload anything seriously offending and I have to hand over their info to authorities. – Lonand – 2012-01-22T00:17:31.703